python 计算列表内容出现次数


"""
python 计算列表内容出现次数
"""
#方法一:
   l = ['a','a','b','c','d','b','b','b']
   test_dict = {}
   for i in l:
        #通过key来计算元素个数
        test_dict[i] = test_dict.get(i,0) + 1
   print(test_dict)

 

使用python中的内置模块

#方法二
l = ['a','a','b','c','d','b','b','b']
from collections import Counter
r = Counter(l)
print(r)



#遍历循坏字典
for key,value in test_dict.items():
print("Key: " + key)
print("Value: " + str(value))
posted @ 2019-05-15 16:56  小酥肉是我  阅读(2617)  评论(0编辑  收藏  举报