2016-09-06 90 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')