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

 

posted @ 2018-03-29 22:23  李荣先辈Java  阅读(2085)  评论(0编辑  收藏  举报