0

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 to merge this my previous month and also 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))])

2 Answers2

1

[1] Concatanate the items (pairs) lists:

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

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

items = sorted(items)

[3] Back to an OrderedDict:

date = OrderedDict(items)

As a one-liner:

date = OrderedDict( sorted( list(date1.items()) + list(date2.items()) )  )
shx2
  • 61,779
  • 13
  • 130
  • 153
  • I think the OP wants to join the values of the same 'day' with the same key. For example - '2015-04-05' has all the non-zero values of 5th of all the months – fixxxer Apr 25 '15 at 06:33
0

It looks like date2 values are all pre-sorted by date. date1 values must follow for the final product. I think the .join function will help.

This answer may help you: How to merge two Python dictionaries in a single expression?

Community
  • 1
  • 1
Propulsion
  • 503
  • 2
  • 4
  • 14