for a list, for example [1,2,2,2,2,3,3,3,4,4,4,4], now I want to count the duplicates in this table and repeat them several times.
EG1:
mylist = [1,2,2,2,2,3,3,3,4,4,4,4]
MySet = set (MyList) #myset是另外一个列表 with no duplicates inside MyList
For item in MySet:
print ("The%d has found%d"% (Item,mylist.count (item)))
EG2:
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)
print (a)
use the characteristics of the dictionary to achieve.
eg3:
>>> from collections import Counter
>>> Counter ([1,2,2,2,2,3,3,3,4,4,4,4])
Counter ({1:5, 2:3, 3:2})
I'll add a method that only implements the list:
l=[1,4,2,4,2,2,5,2,6,3,3,6,3,6,6,3,3,3,7,8,9,8,7,0,7,1,2,4,7,8,9]
count_times = []
For i in L:
Count_times.append (L.count (i))
Print (L[l.index (max (count_times)))
The principle is to record the number of occurrences of each number in the list in its corresponding position, and then use Max to find the most frequently occurring position.
with this code only, there is a drawback, if there are multiple results, the final result of the reality is only the leftmost one, but the solution is very simple
Number of occurrences of duplicates in the Python statistics list