如何在python中比较两个字典并相应地替换值?

时间:2018-08-21 19:12:18

标签: python json python-3.x list dictionary

这是第一本字典:

[{'ITEMNO': None}, {'ITEM_TYPE': None}, {'CASE_QYT': None}, {'MIN_MINMAX_QUANTITY': None}, {'MAX_MINMAX_QUANTITY': None}, {'ONHAND': None}, {'ONHAND_SUBINV': None}, {'LOCATOR': None}, {'ORGANIZATION_ID': None}, {'INVENTORY_ITEM_ID': None}, {'CREATION_DATA': None}, {'INVENTORY_ITEM_STATUS_CODE': None}]

此词典包含所有关键元素,其值为None。我有第二本词典,其中可能包含第一本词典中的所有关键元素,或者少于第一本词典中的所有关键元素,但具有相同的关键元素。我想比较这两个字典,如果键相同,则用第二个字典的value元素替换None的值。第二本字典如下:

[{'ITEM_TYPE': 'SPR_ITEM_CPR'}, {'MIN_MINMAX_QUANTITY': '2'}, {'MAX_MINMAX_QUANTITY': '3'}, {'ONHAND': '162'}, {'ONHAND_SUBINV': 'RSP-SPARES'}, {'LOCATOR': '.RJG005D'}, {'ORGANIZATION_ID': '300000002445681'}, {'INVENTORY_ITEM_ID': '100000001537040'}, {'CREATION_DATE': '01-08-17'}, {'INVENTORY_ITEM_FLAG': 'Y'}, {'INVENTORY_ITEM_STATUS_CODE': 'Active'}]

结果字典应如下所示:

[{'ITEMNO': None}, {'ITEM_TYPE': 'SPR_ITEM_CPR'}, {'CASE_QTY': None}, {'MIN_MINMAX_QUANTITY': '2'}, {'MAX_MINMAX_QUANTITY': '3'}, {'ONHAND': '162'}, {'ONHAND_SUBINV': 'RSP-SPARES'}, {'LOCATOR': '.RJG005D'}, {'ORGANIZATION_ID': '300000002445681'}, {'INVENTORY_ITEM_ID': '100000001537040'}, {'CREATION_DATE': '01-08-17'}, {'INVENTORY_ITEM_FLAG': 'Y'}, {'INVENTORY_ITEM_STATUS_CODE': 'Active'}]

3 个答案:

答案 0 :(得分:0)

原始字典分别为dict1dict2,可以使用dict.get在一行中创建新字典,如下所示:

new_dict = {k: dict2.get(k, v) for k, v in dict1.items()}

或者,因为您知道dict1中的默认值为None,所以您可以使用以下键:

new_dict = {k: dict2.get(k, None) for k in dict1}

或者如果您不关心突变dict1

dict1.update(dict2)

答案 1 :(得分:0)

尝试将l2转换为字典,然后形成所需的输出。

例如:

l1 = [{'ITEMNO': None}, {'ITEM_TYPE': None}, {'CASE_QYT': None}, {'MIN_MINMAX_QUANTITY': None}, {'MAX_MINMAX_QUANTITY': None}, {'ONHAND': None}, {'ONHAND_SUBINV': None}, {'LOCATOR': None}, {'ORGANIZATION_ID': None}, {'INVENTORY_ITEM_ID': None}, {'CREATION_DATA': None}, {'INVENTORY_ITEM_STATUS_CODE': None}]
l2 = [{'ITEM_TYPE': 'SPR_ITEM_CPR'}, {'MIN_MINMAX_QUANTITY': '2'}, {'MAX_MINMAX_QUANTITY': '3'}, {'ONHAND': '162'}, {'ONHAND_SUBINV': 'RSP-SPARES'}, {'LOCATOR': '.RJG005D'}, {'ORGANIZATION_ID': '300000002445681'}, {'INVENTORY_ITEM_ID': '100000001537040'}, {'CREATION_DATE': '01-08-17'}, {'INVENTORY_ITEM_FLAG': 'Y'}, {'INVENTORY_ITEM_STATUS_CODE': 'Active'}]

check_val = dict((i.keys()[0], i.values()[0]) for i in l2)
result = [{i.keys()[0]: check_val.get(i.keys()[0])} for i in l1 ]
print(result)

Python3

check_val = dict((list(i.keys())[0], list(i.values())[0]) for i in l2)
result = [{list(i.keys())[0]: check_val.get(list(i.keys())[0])} for i in l1 ]

输出:

[{'ITEMNO': None},
 {'ITEM_TYPE': 'SPR_ITEM_CPR'},
 {'CASE_QYT': None},
 {'MIN_MINMAX_QUANTITY': '2'},
 {'MAX_MINMAX_QUANTITY': '3'},
 {'ONHAND': '162'},
 {'ONHAND_SUBINV': 'RSP-SPARES'},
 {'LOCATOR': '.RJG005D'},
 {'ORGANIZATION_ID': '300000002445681'},
 {'INVENTORY_ITEM_ID': '100000001537040'},
 {'CREATION_DATA': None},
 {'INVENTORY_ITEM_STATUS_CODE': 'Active'}]

答案 2 :(得分:0)

如果您有两个字典分别为dict1None个值)和dict2(有意义的值),则可以update dict1为:

dict1.update(dict2)

正如其他答案所指出的,请注意,您没有dict,有listdict s,并且您需要先做出命令,您可以这样做:

a_var = [{1 : None}, {2 : 'b'}]
dict1 =  {x.keys()[0]: x.values()[0] for x in a_var}

一旦有了,

dict2 = {1 : 'a', 2 : 'bb', 3 : 'c'}
dict1.update(dict2)

print(dict1)

输出:

{1: 'a', 2: 'bb', 3: 'c'}