How to sort duplicate list by most duplicate occurrences - Python

I have

list1 = ["one", "two", "two", "three", "four" , "five", "five", "five", "six"] 

and Output should be

 list2 = ["five" , "two", "one", "three" , "six"] 
  • "five" is the first element, because list1 has the largest number of entries (3)
  • "two " is the second element, because in list1 there is the next largest number of entries (2)
  • "one" , "three " and "six" have the same fewer occurrences (1), so they are the last in my list2 . In fact, it does not matter in which position they will be like them after the "five" and "two". In short, list2 = ["five" , "two", "six", "three" , "one"] or list2 = ["five" , "two", "three", "one" , "six"] or any other options are accepted.

I could solve this by creating a dictionary to store the number of events, and then create a new list with my items ordered by dict

 my_dict = {i:list1.count(i) for i in list1} 

but i need something cleaner

+5
source share
4 answers

You can use list comprehension and Counter :

 from collections import Counter print([element for element,count in Counter(list1).most_common()]) 

Outputs:

 ['five', 'two', 'three', 'six', 'four', 'one'] 
+6
source

You can use itertools.groupby() to get a list sorted by the number of occurrences:

 from itertools import groupby from operator import itemgetter grouped = [(uniq, len(list(dups))) for uniq, dups in groupby(sorted(list1))] # group & count grouped.sort(key=itemgetter(1), reverse=True) # sort by occurrence list2 = list(map(itemgetter(0), grouped)) 

If duplicates are already grouped in list1 (for example, in your question), you can refuse the call to sorted() . groupby() can be more efficient (memory / time -wise) than collections.Counter() - measure it if it is important in your case.

+1
source

Use collection counter. This is his specific purpose.

https://docs.python.org/2/library/collections.html

0
source
 a= ["one", "two", "two", "three", "four" , "five", "five", "five", "six"] dic={} for name in a: if name in dic: dic[name]=dic[name]+1 else: dic[name]=1 keyList=[] valueList=dic.values() valueList.sort() valueList.reverse() def get_Value(dic,value): for name in dic: if dic[name] == value: del dic[name] return name for num in valueList: keyList.append(get_Value(dic,num)) print keyList 
-1
source

Source: https://habr.com/ru/post/1243227/


All Articles