dict_A = {
'Key A': Value1,
'Key B': Value2
}
dict_B = {
Value1: ValueX,
Value2: ValueY
}
当value-key匹配时,我如何用dict_B中的值替换dict_A中的值?
dict_A = {k: dict_B.get(v, v) for k, v in dict_A.items()}
您可以使用dict.get()
:
dict_A = {"Key A": "Value1", "Key B": "Value2"}
dict_B = {"Value1": "ValueX", "Value2": "ValueY"}
for key, value in dict_A.items():
dict_A[key] = dict_B.get(value, value)
print(dict_A)
打印:
{'Key A': 'ValueX', 'Key B': 'ValueY'}