2016-09-06 88 views
-4

我想知道如何解决这个问题:神交学习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 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 
+3

它看起来像你希望我们为你写一些代码。尽管许多用户愿意为遇险的编码人员编写代码,但他们通常只在海报已尝试自行解决问题时才提供帮助。展示这一努力的一个好方法是包含迄今为止编写的代码,示例输入(如果有的话),期望的输出以及实际获得的输出(输出,回溯等)。您提供的细节越多,您可能会收到的答案就越多。检查[FAQ](http://stackoverflow.com/tour)和[如何提问](http://stackoverflow.com/questions/how-to-ask)。 – TigerhawkT3

回答

0

你必须input多次与警戒值调用,然后计算的对象,然后遍历键和值,然后为每个计数打印格式化的字符串。合理简单的一行代码:

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