Grok学习python编程

Grok学习python编程,python,Python,我想知道如何解决这个问题: 你对最流行和最不流行的汽车颜色很好奇,决定写一个程序来计算汽车颜色的频率 您的程序应该读取每辆车的颜色,直到输入一个空行,然后以任何顺序打印出所有不同颜色的车和计数 例如: Car: red Car: white Car: blue Car: green Car: white Car: silver Car: Cars that are green: 1 Cars that are silver: 1 Cars that are red: 1 Cars that a

我想知道如何解决这个问题:

你对最流行和最不流行的汽车颜色很好奇,决定写一个程序来计算汽车颜色的频率

您的程序应该读取每辆车的颜色,直到输入一个空行,然后以任何顺序打印出所有不同颜色的车和计数

例如:

Car: red
Car: white
Car: blue
Car: green
Car: white
Car: silver
Car: 
Cars that are green: 1
Cars that are silver: 1
Cars that are red: 1
Cars that are white: 2
Cars that are blue: 1
Car: red
Car: white
Car: white
Car: red
Car: white
Car: white
Car: white
Car: 
Cars that are red: 2
Cars that are white: 5
​ 下面是另一个例子:

Car: red
Car: white
Car: blue
Car: green
Car: white
Car: silver
Car: 
Cars that are green: 1
Cars that are silver: 1
Cars that are red: 1
Cars that are white: 2
Cars that are blue: 1
Car: red
Car: white
Car: white
Car: red
Car: white
Car: white
Car: white
Car: 
Cars that are red: 2
Cars that are white: 5
您必须使用sentinel值多次调用input,然后对对象进行计数,然后对键和值进行迭代,然后为每个计数打印格式化字符串。一行代码就相当简单:

print(*('Cars that are {}: {}'.format(*item) for item in __import__('collections').Counter(iter(lambda: input('Car: '), '')).items()), sep='\n')

这应该有帮助

看起来你想让我们为你写些代码。虽然许多用户愿意为陷入困境的程序员编写代码,但他们通常只在海报已经试图自己解决问题时才提供帮助。演示这项工作的一个好方法是包括您迄今为止编写的代码、示例输入(如果有)、预期输出以及您实际获得的输出、回溯等。。你提供的细节越多,你可能得到的答案就越多。检查和。@TigerhawkT3谢谢你指出这一点。