Dictionary manipulation
<p>I have a dictionary of dictionaries which dialed down a notch or two looks like this:</p>

<pre><code>a = {114907: {114905: 1.4351310915,
              114908: 0.84635577943,
              114861: 61.490648372},
     113820: {113826: 8.6999361654,
              113819: 1.1412795216,
              111068: 1.1964946282,
              117066: 1.5595617822,
              113822: 1.1958951003},
     114908: {114906: 1.279878388,
              114907: 0.77568252572,
              114862: 2.5412545474}
     }
</code></pre>

<p>The operation I wanna perform is as follows:</p>

<p>For every key of a:</p>

<ul>
<li>If its value (the innermost dictionary, e.g., <code>{114905: 1.435.., 114908: 0.846.., 114861: 61.490..}</code>) contains keys that are present as keys on the outermost one as well (in this case <code>114908</code>), replace them with the <code>k, v</code> values from the latter and remove it entirely.</li>
<li>Finally, convert the outermost key to a tuple containing both the original key and the key that was popped from the innermost dict.</li>
</ul>

<p>The desired output would be this:</p>

<pre><code>b = {(114907, 114908): {114905: 1.4351310915,
                        114906: 1.279878388,
                        114862: 2.5412545474,
                        114861: 61.490648372},
     113820: {113826: 8.6999361654,
              113819: 1.1412795216,
              111068: 1.1964946282,
              117066: 1.5595617822,
              113822: 1.1958951003}
     }
</code></pre>

<p>I really hope you got what I am trying to achieve here because this is not even describable.</p>

<p>This is what I have so far but it fails in several points and I am deeply convinced that I am going down the wrong road. Eventually I will get there but it would be the most inefficient thing ever coded.</p>

<pre><code>from copy import deepcopy

temp = deepcopy(a)
for item in temp:
    for subitems, values in temp[item].items():
        if values &lt; 1.0:
            for k, v in temp[subitems].items():
                if k != item:
                    a[item][k] = v
#                   a[item].pop(subitems)
for i in a:
    print(i, a[i])
#114908 {114905: 1.4351310915, 114906: 1.279878388, 114907: 0.77568252572, 114861: 61.490648372, 114862: 2.5412545474}
#114907 {114905: 1.4351310915, 114906: 1.279878388, 114908: 0.84635577943, 114861: 61.490648372, 114862: 2.5412545474}
#113820 {113826: 8.6999361654, 113819: 1.1412795216, 111068: 1.1964946282, 117066: 1.5595617822, 113822: 1.1958951003}
</code></pre>

<p>Side question, why does <code>pop</code> in dictionaries return the <code>value</code> only and not the <code>key: value</code> pair?</p>

<p>EDIT</p>

<p>An important detail which might make the thing easier is that another way to look for what has to be modified are the inner dict values. If they are below 1.0 their keys are bound to be keys of the outer dict as well.</p>

<p>How about this:</p>

<pre><code>import itertools

b ={}

for k1,v1 in a.items():
    for k2,v2 in v1.items():
        if k2 in a:
            a[k2].pop(k1)
            a[k1].pop(k2)
            dest = dict(itertools.chain(a[k1].items(), a[k2].items())) #python 2.7

            b[(k1,k2)] = dest

print b
</code></pre>

<p>answer:</p>

<pre><code>{(114908, 114907): {114905: 1.4351310915, 114906: 1.279878388, 114861: 61.490648372, 114862: 2.5412545474}}
</code></pre>

python python-3.x dictionary