在Python中怎样检查一个数字在输入中出现多少次?
比如说,假设有这样一段代码:
numbers=input("Enter numbers: ")
如果有人输入了11234458881这个数字
我该怎么做才能得到这样的输出:
1出现了3次
2出现了1次
3出现了1次
4出现了2次
以此类推
1 个回答
6
为什么不使用Counter:
from collections import Counter
Counter("11234458881")
返回:
Counter({'1': 3, '8': 3, '4': 2, '3': 1, '2': 1, '5': 1})