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.
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.
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.
>>> 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
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)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With