以下はあなたが望むことをします:
from collections import defaultdict
d1 = defaultdict(list, {'A': [4, 4, 4, 4], 'S': [1], 'C': [1, 2, 3, 4]})
print 'the d1 is ', d1
d2 = defaultdict(list, {'A': [4, 4, 4], 'B': [2], '[]': [4, 4], 'C': [1, 2, 3]})
print 'the d2 is ', d2
d3 = defaultdict(list, dict((key, set(value) if len(value) > 1 else value)
for key, value in d1.iteritems()))
d3.update((key, list(d3[key].union(set(value)) if key in d3 else value))
for key, value in d2.iteritems())
print
print 'the d3 is ', d3
出力:
the d1 is defaultdict(<type 'list'>, {'A': [4, 4, 4, 4], 'S': [1], 'C': [1, 2, 3, 4]})
the d2 is defaultdict(<type 'list'>, {'A': [4, 4, 4], 'C': [1, 2, 3], 'B': [2], '[]': [4, 4]})
the d3 is defaultdict(<type 'list'>, {'A': [4], 'S': [1], 'B': [2], 'C': [1, 2, 3, 4], '[]': [4, 4]})
'C'
私は両方d1
にキーを付けたリストを追加しd2
、あなたの質問に記載されていない可能性に対して何が起こるかを示すことに注意してください-それがあなたが起こりたいことであるかどうかはわかりません。