擅长:python、mysql、java
<p>使用<a href="https://docs.python.org/3/library/itertools.html#itertools.chain" rel="nofollow">^{<cd1>}</a>获取dicts中所有键的iterable。你知道吗</p>
<pre><code>from itertools import chain
def h(a, b):
return a > b
d2 = {1:40, 2:50, 3:60, 4:70, 6:90}
d1 = {1:30, 2:20, 3:30, 5:80}
def dict_interdiff(d1, d2):
dict1 = {}
dict2 = {}
for key in set(chain(d1, d2)):
if key in d1 and key in d2:
dict1[key] = h(d1[key], d2[key])
else:
dict2[key] = d1.get(key) or d2.get(key)
return dict1, dict2
</code></pre>