Returning the top 6 names in a list of tuples in Python

Please, I want to return the first 6 names (names only) with the highest corresponding integers from the list of tuples below. I was able to return all the names from the highest (SMS) to the lowest (boss).

[('sms', 10), ('bush', 9), ('michaels', 7), ('operations', 6), ('research', 5), ('code', 4), ('short', 3), ('ukandu', 2), ('technical', 1), ('apeh', 1), ('boss', 1)] 

Thanks.

+7
source share
3 answers

heapq.nlargest is what you need:

 import heapq from operator import itemgetter largest_names = [x[0] for x in heapq.nlargest(6,your_list,key=itemgetter(1))] 

It will be more efficient than sorting, as it only accepts the largest elements and discards the rest. Of course, it is less efficient than slicing if the list is pre-sorted for other reasons.

Complexity:

  • heapq: O (N)
  • sort: O (NlogN)
  • slicing (only if pre-sorted): O (6)

Explanation:

 heapq.nlargest(6,your_list,key=itemgetter(1)) 

This line returns a list of tuples (name, value), but only the 6 largest ones - the comparison is performed by the second (index = 1 β†’ key=itemgetter(1) ) element in the tuple.

The rest of the line is understanding the list by the 6 least name, tuples of values ​​that take only part of the tuple name and store it in the list.


You may be interested that you can save this data as collections.Counter .

 d = collections.Counter(dict(your_list)) biggest = [x[0] for x in d.most_common(6)] 

Probably you should not convert just for this calculation (which is because heapq is for everyone ;-), but it may be advisable to convert to make it easier to work with data.

+11
source
 data=[('sms', 10), ('bush', 9), ('michaels', 7), ('operations', 6), ('research', 5), ('code', 4), ('short', 3), ('ukandu', 2), ('technical', 1), ('apeh', 1), ('boss', 1)] return [x[0] for x in sorted(data, key=lambda x: x[1], reverse=True)[0:6]] 

Which does the following:

  • sorted returns data sorted using the key function. As the standard sort order increases, reverse=True sets it to go down,
  • lambda x: x[1] - an anonymous function that returns the second element of the argument (tuple in this case); itemgetter(1) is the best way to do this, but requires additional imports;
  • [0:6] first truncates 6 list items;
  • [x[0] for x in ... ] creates a list of the first elements of each transferred tuple;
+4
source

If the data is already sorted, just cut off the first six tuples, and then get the names:

 first_six = data[0:6] # or data[:6] only_names = [entry[0] for entry in first_six] 

The explanation of the list can be expanded to:

 only_names = [] for entry in first_six: only_names.append(entry[0]) 

If the list is not yet sorted, you can use the key keyword argument of the sort method (or the built-in sorted ) to sort by the result:

 data.sort(key=lambda entry: entry[1], reverse=True) 

lambda - anonymous function - equivalent:

 def get_score(entry): return entry[1] data.sort(key=get_score, reverse=True) 
+1
source

All Articles