方法一
使用list的内置方法list.count()
l = [1, 1, 2, 3, 3]
sl = set(l)
for i in sl:
if l.count(i) > 1:
print("元素{},重复{}次".format(i, l.count(i)))
方法二
使用python内置方法collections的Count()
模块
from collections import Counter
l = [1, 1, 2, 3, 3]
cl = Counter(l)
for k, v in cl.items():
if v > 1:
print("元素{}, 重复{}次".format(k, v))
方法三
使用for循环
l = [1, 1, 2, 3, 3]
sl = set(l)
d = {}
for s in sl:
count = 0
for i in l:
if i == s:
count += 1
d[s] = count
for k, v in d.items():
if v > 1:
print("元素{}, 重复{}次".format(k, v))
参考链接:Python列表统计重复元素