calculate the total value dict
<p>I have a problem, how to calculate the total dict of the same keys ? I have a dict:</p>

<pre><code>{'learning': {'DOC1': 0.14054651081081646,
              'DOC2': 0,
              'DOC3': 0.4684883693693881},
 'life':     {'DOC1': 0.14054651081081646, 
              'DOC2': 0.20078072972973776, 
              'DOC3': 0}
}
</code></pre>

<p>and I hope the results as:</p>

<pre><code>{'learning life': {
        'DOC1': DOC1 in learning + DOC1 in life,
        'DOC2': DOC2 in learning + DOC2 in life,
        'DOC3': DOC3 in learning + DOC3 in life,}}
</code></pre>

<p>Thank you very much</p>

<p>Pretty simple:</p>

<pre><code>for k in d['learning']:
    print(d['learning'][k] + d['life'][k])
</code></pre>

<p>... with <code>d</code> being your <code>dict</code> and no error checking whatsoever (does the key exist, is it really a number, etc.).
<hr>
As whole code snippet with a comprehension:</p>

<pre><code>d = {'learning': {'DOC1': 0.14054651081081646,
              'DOC2': 0,
              'DOC3': 0.4684883693693881},
 'life':     {'DOC1': 0.14054651081081646, 
              'DOC2': 0.20078072972973776, 
              'DOC3': 0}
}

d['sum'] = [d['learning'][k] + d['life'][k]
            for k in d['learning']]
print(d)
</code></pre>

<p>See <a href="http://ideone.com/BDmyrw" rel="nofollow"><strong>a demo on ideone.com</strong></a>.</p>

python dictionary tf-idf