Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine two array's data using inner join

I've two data sets in array:

arr1 = [
  ['2011-10-10', 1, 1],
  ['2007-08-09', 5, 3],
  ...
]

arr2 = [
  ['2011-10-10', 3, 4],
  ['2007-09-05', 1, 1],
  ...
]

I want to combine them into one array like this:

arr3 = [
  ['2011-10-10', 1, 1, 3, 4],
  ...
]

I mean, just combine those lines with the same date column.

EDIT

Thanks everyone, Just for clarification, I don't need those lines which not appear in both array, just drop them.

like image 914
WoooHaaaa Avatar asked Jul 16 '13 17:07

WoooHaaaa


1 Answers

Organize your data differently (you can easily convert what you already have to two dicts):

d1 = { '2011-10-10': [1, 1],
       '2007-08-09': [5, 3]
     }
d2 = { '2011-10-10': [3, 4],
       '2007-09-05': [1, 1]
     }

Then:

d3 = { k : d1[k] + d2[k] for k in d1 if k in d2 }
like image 172
jason Avatar answered Sep 16 '22 18:09

jason