Python笔记(二)查找重复元素
1、查找重复项:
1) 使用内置count()函数查找重复项:
>>>list = [1,2,3,2,1] >>>for i in l: ....... print("%s has found %s" % (i, l.count(i))) #1 has found 2 #2 has found 2 #3 has found 1 #2 has found 2 #1 has found 2
2)
list = [1,2,3,2,1] s = set(list) for i in s: print("%s has found %s" % (i, l.count(i))) #1 has found 2 #2 has found 2 #3 has found 1