嗨,如果键相同,我想合并两个字典的值。
DIC_01
{'A': ['Zero'],
'B': ['Zero'],
'C': ['Zero'],
'D': ['Zero']}
DIC_02
{'A': [2338.099365234375,
-3633.070068359375,
-73.45938873291016],
'D':[2839.291015625,
-2248.350341796875,
1557.59423828125]}
想法输出
{'A': [[2338.099365234375,
-3633.070068359375,
-73.45938873291016],['Zero']],
'D': [[2839.291015625,
-2248.350341796875,
1557.59423828125]['Zero']]}
找不到键的输出
{'B': ['Zero'],'C': ['Zero']}
我尝试过
NO_MATCH={}
for k in DIC_01.keys():
DOC={}
for k2 in DIC_02.keys():
if k == k2:
DOC = k.values().update(k2.values())
else:
NO_MATCH.update(DIC_01)
DOC中没有任何内容,所有词典元素都位于NO_MATCH中,没有错误消息。不知道哪里出了问题,我也认为必须有更好的方法来做到这一点。 谢谢!
答案 0 :(得分:1)
编辑:您可以声明两个单独的字典,并遍历两个字典的所有键。对于每次迭代,请检查密钥是否在DIC_02
和DIC_01
中并连接两个对应的列表
match, no_match = {}, {}
for i in {**DIC_01,**DIC_02}.keys():
if i in DIC_01 and i in DIC_02:
match[i] = DIC_01[i] + DIC_02[i]
else:
no_match[i] = DIC_01.get(i,[]) + DIC_02.get(i,[])
答案 1 :(得分:0)
这不是特别花哨,但这应该可以解决您要寻找的问题
def merge(a, b):
out = {}
for key in a.keys() | b.keys():
if key in a and key in b:
out[key] = [a[key], b[key]]
elif key in a:
out[key] = a[key]
else:
out[key] = b[key]
return out
其中a
和b
是dict
s。 |
采用两个密钥集的并集。
答案 2 :(得分:0)
关于代码为什么出错。
NO_MATCH={}
for k in DIC_01.keys():
DOC={} # (1)
for k2 in DIC_02.keys(): # (2)
if k == k2:
DOC = k.values().update(k2.values()) # (3)
else:
NO_MATCH.update(DIC_01) # (4)
A
中的DIC_01
与A
,DIC_02
中的"ok, A matched"
进行比较。但是,它将继续比较A
中的DIC_01
和D
中的DIC_02
,"ok, A not found in DIC_02, not matched, add to NOT_MATCH"
,这是错误的,因为A
是匹配的密钥并且A
位于DIC_02
中。DIC_01
添加到NO_MATCH
,错了!FIX:
MATCH = {}
NO_MATCH = {}
# This goes through all keys in DIC_01. If a key is also found in DIC_02,
# it's a "matched" key so it adds that key to the MATCH variable. If it's
# not in DIC_02, it's a "no matched" key -> add key to NO_MATCH variable.
for k in DIC_01.keys():
if k in DIC_02.keys():
MATCH[k] = [DIC_01[k], DIC_02[k]]
else:
NO_MATCH[k] = DIC_01[k]
# BUT...We are still missing the keys that are only in DIC_02. So we need
# another loop
for k in DIC_02.keys():
if k not in DIC_01.keys():
NO_MATCH[k] = DIC_02[k]
# This is the same as the loop above, without the if block.
更好的方式
一些列表理解可以使事情保持清洁。
MATCH = {key:[DIC_01[key], DIC_02[key]] for key in DIC_01 if key in DIC_02}
unmatch_1 = {key:DIC_01[key] for key in DIC_01 if key not in DIC_02}
unmatch_2 = {key:DIC_02[key] for key in DIC_02 if key not in DIC_01}
NOT_MATCH = {**unmatch_1, **unmatch_2}
说明:
MATCH = {key:[DIC_01[key], DIC_02[key]] for key in DIC_01 if key in DIC_02}
这将用英语创建一个名为match
的新字典。对于DIC_01
中的每个键,如果该键也位于DIC_02
中,则在{{1}中创建相同的键},并从match
和DIC_01
中分配该键的值。
DIC_02
此...对于unmatch_1 = {key:DIC_01[key] for key in DIC_01 if key not in DIC_02}
中的每个键,如果不在DIC_01
中,则创建一个键并从DIC_02
分配关联的值
DIC_01
此...对于unmatch_2 = {key:DIC_02[key] for key in DIC_02 if key not in DIC_01}
中的每个键,如果不在DIC_02
中,则创建一个键并从DIC_01
分配关联的值
DIC_02
这是合并2个字典的一种很酷的方法(仅适用于Python 3.5及更高版本)
答案 3 :(得分:-1)
>>> a={'A': ['Zero'],
... 'B': ['Zero'],
... 'C': ['Zero'],
... 'D': ['Zero']}
>>> b={'A': [2338.099365234375,
... -3633.070068359375,
... -73.45938873291016],
... 'D':[2839.291015625,
... -2248.350341796875,
... 1557.59423828125]}
>>> map=ChainMap(b,a)
>>> map['A']
[2338.099365234375, -3633.070068359375, -73.45938873291016]
>>> map['C']
['Zero']
键的优先级将按照字典的顺序排列,因此,如果您无法控制顺序,或者['Zero']
混合并匹配:这种方法无济于事。