2013-03-13 60 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}) 
相关问题