Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>&gt;&gt;&gt; dict1 = dict(arrayOne) &gt;&gt;&gt; dict2 = dict(arrayTwo) &gt;&gt;&gt; keyset = set(dict1.keys() + dict2.keys()) &gt;&gt;&gt; [[key, dict1.get(key, 0), dict2.get(key, 0)] for key in keyset] [['james', 35, 36], ['robert', 12, 0], ['charles', 0, 45], ['michael', 28, 17], ['trevor', 0, 24], ['jack', 18, 0], ['steven', 23, 4]] </code></pre> <p>This gets a bit more complicated if you want to add multiple columns; a dictionary is then best. But having <code>0</code>s in the right places becomes a challenge, because when we add a name to the "master dictionary", we have to make sure it starts with a list of <code>0</code>s of the right length. I'm tempted to create a new class for this, but first, here's a basic function-based solution:</p> <pre><code>def add_column(masterdict, arr): mdlen = len(masterdict[masterdict.keys()[0]]) newdict = dict(arr) keyset = set(masterdict.keys() + newdict.keys()) for key in keyset: if key not in masterdict: masterdict[key] = [0] * mdlen masterdict[key].append(newdict.get(key, 0)) arrayOne = [["james", 35], ["michael", 28], ["steven", 23], ["jack", 18], ["robert", 12]] arrayTwo = [["charles", 45], ["james", 36], ["trevor", 24], ["michael", 17], ["steven", 4]] arrayThree = [["olliver", 11], ["james", 39], ["john", 22], ["michael", 13], ["steven", 6]] masterdict = dict([(i[0], [i[1]]) for i in arrayOne]) add_column(masterdict, arrayTwo) print masterdict add_column(masterdict, arrayThree) print masterdict </code></pre> <p>Output:</p> <pre><code>{'james': [35, 36], 'robert': [12, 0], 'charles': [0, 45], 'michael': [28, 17], 'trevor': [0, 24], 'jack': [18, 0], 'steven': [23, 4]} {'james': [35, 36, 39], 'robert': [12, 0, 0], 'charles': [0, 45, 0], 'michael': [28, 17, 13], 'trevor': [0, 24, 0], 'olliver': [0, 0, 11], 'jack': [18, 0, 0], 'steven': [23, 4, 6], 'john': [0, 0, 22]} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload