python中統計列表中元素出現的次數


 

>>> test1 = ["aaa","bbb","aaa","aaa","ccc","ccc","ddd","aaa","ddd","eee","ddd"] >>> test1.count("aaa") ## 統計指定元素出現的次數 4
>>> test1.count("bbb") 1
>>> test2 = [] >>> for i in test1: ## 去重復 if i not in test2: test2.append(i) >>> test2 ['aaa', 'bbb', 'ccc', 'ddd', 'eee'] >>> for i in test2: ## 統計每一個元素出現的次數 print(f"{i}:{test1.count(i)}") aaa:4 bbb:1 ccc:2 ddd:3 eee:1
>>> 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM