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

 

posted @ 2020-12-18 23:33  小鲨鱼2018  阅读(3768)  评论(0编辑  收藏  举报