Search code examples
pythonarraysarrayofarrays

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.

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


Solution

  • 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 }