Python dictionary comparison
<p>Problem:<br>
I am trying to implement distance vector routing protocol and I need to track distances of Nodes/Routers (A, B, C) and distances from their neighbors (1,2,3) and update the best path (source router to destination router) if one of the router learns about another best path from its neighbors by processing and sending updated distance vectors. More details here en.wikipedia.org/wiki/Distance-vector_routing_protocol</p>

<p>I am trying to compare two dictionaries (<code>a</code> &amp; <code>b</code>) and if I find any of the keys of <code>b</code> (i.e. <code>'B'</code>) present in <code>a</code> then I want to add the value of <code>'B'</code> (i.e. <code>1</code>) from <code>a</code> in to <code>'C'</code> (i.e. <code>2</code>) from <code>b</code> so the output looks similar to following:</p>

<pre><code>a = {'A': {'B': 1}}

b = {'B': {'C': 2}}
</code></pre>

<p>Final Output:</p>

<pre><code>a = {'A': {'B': 1, 'C': 3}}
</code></pre>

<p>This is not possible with dictionaries, but with sets it is. For example, </p>

<pre><code>s = {'K':'L', 'L':'K', 'Q':'P'}
p = {'K':'L', 'Q':'P'}

# for python 3
k = s.values() &amp; p.values() # k is now {'L', 'P'}
# for python 2
k = s.viewvalues() &amp; p.viewvalues() # is now {'L', 'P'}
</code></pre>

<p>Look at set documentation <a href="https://docs.python.org/2/library/stdtypes.html#set" rel="nofollow">here</a></p>

python