网络编程
位置:首页>> 网络编程>> Python编程>> Python3查找列表中重复元素的个数的3种方法详解

Python3查找列表中重复元素的个数的3种方法详解

作者:猪笨是念来过倒  发布时间:2022-08-01 16:36:07 

标签:Python,列表,重复

方法一:


mylist = [1,2,2,2,2,3,3,3,4,4,4,4]
myset = set(mylist)
for item in myset:
 print("the %d has found %d" %(item,mylist.count(item)))

the 1 has found 1

the 2 has found 4

the 3 has found 3

the 4 has found 4

方法二:


from collections import Counter
Counter([1,2,2,2,2,3,3,3,4,4,4,4])
Counter({2: 4, 4: 4, 3: 3, 1: 1})

方法三:


List=[1,2,2,2,2,3,3,3,4,4,4,4]
a = {}
for i in List:
 if List.count(i)>1:
   a[i] = List.count(i)

来源:https://blog.csdn.net/liao392781/article/details/87257097

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com