加载中

Robin
Python

python 统计list中元素重复的次数

2022/12/29 · 1 min read

方法一:通过 set 去重,再用 count 计数

names = ['a', 'b', 'c', 'd', 'c', 'a', 'c']
names_a = list(set(names))
{i: names.count(i) for i in names_a for i in names}

方法二:通过 collections 来计数

import collections
 
countdic = collections.Counter(['a', 'b', 'c', 'd', 'c', 'a', 'c'])
print(countdic)

相关文章