2013-03-13 61 views
0

例如,讓說在Python中,我怎樣才能檢查一個數字在輸入中出現的次數?

numbers=input("Enter numbers: ") 

如果有人輸入11234458881

我怎樣才能使輸出

1出現3次

2出現1次

3出現1時間

4出現2次

等等

+1

使用輸入像數組,看看相關的問題的http:// stackoverflow.com/questions/991350/counting-repeated-characters-in-a-string-in-python – user1929959 2013-03-13 23:41:01

+2

這個術語是(非圖形)[直方圖](http://en.wikipedia.org/wiki /直方圖),這應該有助於* se拱門*。 – 2013-03-13 23:43:54

回答

6

爲什麼不使用計數器:

from collections import Counter 
Counter("11234458881") 

回報:

Counter({'1': 3, '8': 3, '4': 2, '3': 1, '2': 1, '5': 1}) 
相關問題