识别数字列表或数字元组里的重复内容并转成字典
count_num = repeat_count([1, 2, 3, 2, 4, 5, 2, 3, 2, 5]) 会出现
{1:1, 2:4, 3:2, 4:1, 5:2} # there is 1 one, 4 twos, 2 threes, 1 four, and 2 fives
可任选一种
def repeat_count(it):
dic = {}
for i in sorted(list(it)):
dic[i] = dic.get(i, 0) + 1
return dic
count_num = repeat_count([1, 2, 3, 2, 4, 5, 2, 3, 2, 5])
print(count_num) # {1:1, 2:4, 3:2, 4:1, 5:2}
from collections import Counter
def repeat_count(it):
return dict(Counter(it))
count_num = repeat_count([1, 2, 3, 2, 4, 5, 2, 3, 2, 5])
print(count_num) # {1:1, 2:4, 3:2, 4:1, 5:2}