BruceWayne
BruceWayne

Reputation: 23283

Sort dict within dict by Month

I have the following dictionary:

myList= {'Green Cars': {'April': 11455.0, 'March': 12210, 'February': 2559, 'August': 1439, 'July': 4921, 'January': 340, 'December': 340, 'November': 340, 'October': 306, 'September': 6}, 
'Yellow cars': {'April': 192914, 'March': 20187, 'February': 20994, 'January': 230625, 'December': 208422, 'November': 17914, 'October': 170835, 'September': 185357, 'July': 226697, 'May': 1852, 'August': 22096}, 
'Red Cars': {'April': 159, 'March': 1400, 'February': 2620, 'January': 237, 'August': 2959, 'July': 6943},...

How do I sort each primary key ("Green Cars", "Yellow Cars", "Red Cars", etc. by month?

I've seen how to sort a dictionary by an internal dictionary where the secondary key is the same, but not when it changes.

In the end, I'm sorting these all to get a chart by month, for different cars via matplotlib.

ie. enter image description here

Upvotes: 3

Views: 499

Answers (1)

dawg
dawg

Reputation: 103744

You have two issues: Sorting the nested dictionary of dicts and maintaining the sorted result in a dict. In Python, dicts are unordered.

To sort the dict of dicts that you used as an example:

import calendar 
months=list(calendar.month_name)       # use a dict if you have lots of data...
myDictODicts= {'Green Cars': {'April': 11455.0, 'March': 12210, 'February': 2559, 'August': 1439, 'July': 4921, 'January': 340, 'December': 340, 'November': 340, 'October': 306, 'September': 6}, 
'Yellow cars': {'April': 192914, 'March': 20187, 'February': 20994, 'January': 230625, 'December': 208422, 'November': 17914, 'October': 170835, 'September': 185357, 'July': 226697, 'May': 1852, 'August': 22096}, 
'Red Cars': {'April': 159, 'March': 1400, 'February': 2620, 'January': 237, 'August': 2959, 'July': 6943}}

for k in myDictODicts:
    print(k, sorted(myDictODicts[k].items(), key=lambda t: months.index(t[0])))

Prints:

Green Cars [('January', 340), ('February', 2559), ('March', 12210), ('April', 11455.0), ('July', 4921), ('August', 1439), ('September', 6), ('October', 306), ('November', 340), ('December', 340)]
Yellow cars [('January', 230625), ('February', 20994), ('March', 20187), ('April', 192914), ('May', 1852), ('July', 226697), ('August', 22096), ('September', 185357), ('October', 170835), ('November', 17914), ('December', 208422)]
Red Cars [('January', 237), ('February', 2620), ('March', 1400), ('April', 159), ('July', 6943), ('August', 2959)]

You can either use the list of tuples or recreate a dict directly using those. With Python 3.6, you can turn that sorted list of tuples back into a dict that will maintain its insertion order. You can also use an OrderedDict that will appear to the rest of your code as a dict would.

Example:

from collections import OrderedDict
nd={}
for k in myDictODicts:
    nd[k]=OrderedDict(sorted(myDictODicts[k].items(), key=lambda t: months.index(t[0])))

>>> nd
{'Green Cars': OrderedDict([('January', 340), ('February', 2559), ('March', 12210), ('April', 11455.0), ('July', 4921), ('August', 1439), ('September', 6), ('October', 306), ('November', 340), ('December', 340)]), 'Yellow cars': OrderedDict([('January', 230625), ('February', 20994), ('March', 20187), ('April', 192914), ('May', 1852), ('July', 226697), ('August', 22096), ('September', 185357), ('October', 170835), ('November', 17914), ('December', 208422)]), 'Red Cars': OrderedDict([('January', 237), ('February', 2620), ('March', 1400), ('April', 159), ('July', 6943), ('August', 2959)])}

Upvotes: 3

Related Questions