1 not only can count the number of occurrences of the elements in list, you can also count on the elements of str
# The Collections package Counter for statistical str list of elements in the number of occurrences from the Collections Import Counter a = [1,1,2,3,4,5,6,6,6] B = Counter (a) # outputs a class a number of occurrences of each element, and the descending order according to the number of occurrences Print (B) # output element 5 occurrences of Print (B [5 ]) # output Occurrences the three (element number) pair, and the type is List Print (b.most_common (. 3 )) # Counter (. 6 {:. 3,. 1: 2, 2:. 1,. 3:. 1,. 4:. 1,. 5:. 1 }) # 1 # [(6, 3), (1, 2), (2, 1)] # Directly as the output element by the number of descending sort occur with most_common () object of the Counter (a) into a List, facilitate iterative Print ([Item for items, C in Counter (a) .most_common () for Item in [items] * C]) # [. 6,. 6,. 6,. 1,. 1, 2,. 3,. 4,. 5]
Reference: http: //www.aiisen.com/p/1166376.html