Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort dictionary of lists by key value pairs

I am trying to sort values that are inside a dictionary of lists and create a new list out of them. Here is the data:

{
    'fbi': [229, 421, 586, 654, 947, 955, 1095, 1294, 1467, 2423, 3063, 3478, 3617, 3730, 3848, 3959, 4018, 4136, 4297, 4435, 4635, 4679, 4738, 5116, 5211, 5330, 5698, 6107, 6792, 6906, 7036], 
    'comey': [605, 756, 1388, 1439, 1593, 1810, 1959, 2123, 2506, 3037, 6848], 
    'hillary': [14, 181, 449, 614, 704, 1079, 1250, 2484, 2534, 2659, 3233, 3374, 3488, 3565, 4076, 4756, 4865, 6125, 7109]
}

What I am trying is to find the 20 smallest values in these and get a list of their corresponding keys. For example, the first three least values are 14(hillary), 181(hillary) and 229(fbi). Therefore, how can I get a list like this:

['hillary', 'hillary', 'fbi']

All values will always be different. Also, all the values in the list are sorted, from ascending to descending.

Here is what I have tried:

for m in range(1,20):
    for i in sort_vals.values():
        if i[0] < a[0]:
            a[0] = i[0]

This gives me the least value but not any other as after one iteration, the least value is always the same. I guess if I can delete that particular value it will be helpful. Can't think of anything else. Thanks!

like image 924
Shawn Avatar asked Aug 26 '19 12:08

Shawn


People also ask

How do you sort dictionaries by dictionary value?

To sort a list of dictionaries according to the value of the specific key, specify the key parameter of the sort() method or the sorted() function. By specifying a function to be applied to each element of the list, it is sorted according to the result of that function.

How do you sort a dictionary by key-value in Python?

To sort a dictionary by value in Python you can use the sorted() function. Python's sorted() function can be used to sort dictionaries by key, which allows for a custom sorting method. sorted() takes three arguments: object, key, and reverse. Dictionaries are unordered data structures.

Can we sort a dictionary with keys in Python?

Python offers the built-in keys functions keys() and values() functions to sort the dictionary. It takes any iterable as an argument and returns the sorted list of keys. We can use the keys to sort the dictionary in the ascending order.


2 Answers

You could flatten the dictionary (d here) into a list of tuples with the corresponding key/value pairs, and sort the tuples according to the values:

from operator import itemgetter

l = [(k,i) for k,v in d.items() for i in v]
# [('fbi', 229), ('fbi', 421), ('fbi', 586), ('fbi', 654),...
list(zip(*sorted(l, key=itemgetter(1))[:3]))[0]
# ('hillary', 'hillary', 'fbi')
like image 73
yatu Avatar answered Nov 02 '22 20:11

yatu


you could

  1. invert your mapping, creating a dictionary with numbers => list of names
  2. sort this dictionary (as tuple)
  3. pick the 3 first items

like this:

import collections

d = collections.defaultdict(list)
data = {'fbi': [229, 421, 586, 654, 947, 955, 1095, 1294, 1467, 2423, 3063, 3478, 3617, 3730, 3848, 3959, 4018, 4136, 4297, 4435, 4635, 4679, 4738, 5116, 5211, 5330, 5698, 6107, 6792, 6906, 7036], 'comey': [605, 756, 1388, 1439, 1593, 1810, 1959, 2123, 2506, 3037, 6848], 'hillary': [14, 181, 449, 614, 704, 1079, 1250, 2484, 2534, 2659, 3233, 3374, 3488, 3565, 4076, 4756, 4865, 6125, 7109]}

for k,vlist in data.items():
    for v in vlist:
        d[v].append(k)

result = [v[0] for k,v in sorted(d.items())[:3]]

print(result)

this prints:

['hillary', 'hillary', 'fbi']

note that if there are several names attached to a value, this code will pick only the first one (v[0])

like image 40
Jean-François Fabre Avatar answered Nov 02 '22 20:11

Jean-François Fabre