擅长:python、mysql、java
<p>这里有两个函数来创建一个“主字典”,它只包含所有输入字典共有的值。您给出的X,Y,Z示例没有所有三个字典共有的值,因此最终会得到一个空字典。你知道吗</p>
<pre><code>X = {'0':[1, 3, 4, 5], '.001':[2, 4, 6, 7]}
Y = {'0':[5, 6, 9, 2], '.001':[2, 6, 8, 4]}
Z = {'0':[3, 6, 8, 9], '.001':[3, 1, 5, 8]}
dictionaries = [X,Y,Z]
def compare(d1, d2):
d3 = {}
for key in d1:
for v in d1[key]:
if v in d2[key]:
if key in d3:
d3[key].append(v)
else: d3[key] = [v,]
return d3
def createMaster(dictionaries):
length = len(dictionaries)
if length == 1:
return dictionaries[0]
d1 = dictionaries.pop()
d2 = dictionaries.pop()
d3 = compare(d1, d2)
if length == 2:
return = d3
else:
dictionaries.append(d3)
return createMaster(dictionaries)
</code></pre>