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))


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM