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