Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting a defaultdict by value in python

I have a data-structure which is something like this:

The population of three cities for different year are as follows.

Name  1990 2000 2010 A     10   20   30 B     20   30   10 C     30   10   20 

I am using a defaultdict to store the data.

from collections import defaultdict cityPopulation=defaultdict(list) cityPopulation['A']=[10,20,30] cityPopulation['B']=[20,30,10] cityPopulation['C']=[30,10,20] 

I want to sort the defaultdict based on a particular column of the list (the year). Say, sorting for 1990, should give C,B,A, while sorting for 2010 should give A,C,B.

Also, is this the best way to store the data? As I am changing the population values, I want it to be mutable.

like image 736
imsc Avatar asked Apr 17 '12 15:04

imsc


People also ask

Can you sort a dictionary by 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.

How do you sort a dictionary by value in Python descending?

Sorting a dict by value descending using list comprehension. The quickest way is to iterate over the key-value pairs of your current dict and call sorted passing the dictionary values and setting reversed=True . If you are using Python 3.7, regular dict s are ordered by default.


2 Answers

>>> sorted(cityPopulation.iteritems(),key=lambda (k,v): v[0],reverse=True) #1990 [('C', [30, 10, 20]), ('B', [20, 30, 10]), ('A', [10, 20, 30])] >>> sorted(cityPopulation.iteritems(),key=lambda (k,v): v[2],reverse=True) #2010 [('A', [10, 20, 30]), ('C', [30, 10, 20]), ('B', [20, 30, 10])] 

Note in python 3 you can't automagically unpack lambda arguments so you would have to change the code

sorted(cityPopulation.items(), key=lambda k_v: k_v[1][2], reverse=True) #2010 
like image 121
jamylak Avatar answered Sep 21 '22 14:09

jamylak


If you want to sort based on the values, not in the keys, use data.items() and set the key with lambda kv: kv[1] so that it picks the value.


See an example with this defaultdict:

>>> from collections import defaultdict >>> data = defaultdict(int) >>> data['ciao'] = 17 >>> data['bye'] = 14 >>> data['hello'] = 23  >>> data defaultdict(<type 'int'>, {'ciao': 17, 'bye': 14, 'hello': 23}) 

Now, let's sort by value:

>>> sorted(data.items(), lambda kv: kv[1]) [('bye', 14), ('ciao', 17), ('hello', 23)] 

Finally use reverse=True if you want the bigger numbers to come first:

>>> sorted(data.items(), lambda kv: kv[1], reverse=True) [('hello', 23), ('ciao', 17), ('bye', 14)] 

Note that key=lambda(k,v): v is a clearer (to me) way to say key=lambda(v): v[1], only that the later is the only way Python 3 allows it, since auto tuple unpacking in lambda is not available.

In Python 2 you could say:

>>> sorted(d.items(), key=lambda(k,v): v) [('bye', 14), ('ciao', 17), ('hello', 23)] 
like image 21
fedorqui 'SO stop harming' Avatar answered Sep 21 '22 14:09

fedorqui 'SO stop harming'