方法一

使用list的内置方法list.count()

1
2
3
4
5
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()模块

1
2
3
4
5
6
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循环

1
2
3
4
5
6
7
8
9
10
11
12
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))