python - Order dict merge by previous date -


my dict is

date1 = ordereddict([('2015-04-18', (5,0)), ('2015-04-05', (33,0)), ('2015-04-15', (3,0)), ('2015-04-08', (53,0))]) date2 = ordereddict([('2015-03-01', (0,5)), ('2015-03-05', (0,10)), ('2015-03-15', (0,13)), ('2015-03-28', (0,53))]) 

i wanted merge previous month , in sorted like,

date = ordereddict([('2015-03-01', (0,5)),('2015-03-28', (0,53)), ('2015-04-05', (33,10)), ('2015-04-08', (53,0), ('2015-04-15', (3,13),('2015-04-18', (5,0))]) 

[1] concatanate items (pairs) lists:

items = list(date1.items()) + list(date2.items()) 

[2] sort them (correctly sorts first in each pair, i.e. date string):

items = sorted(items) 

[3] ordereddict:

date = ordereddict(items) 

as one-liner:

date = ordereddict( sorted( list(date1.items()) + list(date2.items()) )  ) 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -