根据键值合并字典列表

时间:2019-05-21 09:24:38

标签: python-3.x list dictionary

我有两个字典列表,其中我必须根据键值合并两个列表。

a = [{'site': 'KRM Plaza South Tower', 'building': 'd-block', 'level': 'reception', 'gw_mac': 'b827eb36fb0b_1', 'mac': 'e200383d1149a4c975a59618', 'rssi': -63.116279069767444}, {'site': 'KRM Plaza South Tower', 'building': 'd-block', 'level': 'reception', 'gw_mac': 'b827eb36fb0b_1', 'mac': 'e200383d1149a90975a59629', 'rssi': -61.5}, {'site': 'KRM Plaza South Tower', 'building': 'd-block', 'level': 'reception', 'gw_mac': 'b827eb36fb0b_2', 'mac': 'e200383d1149a90975a59629', 'rssi': -59.086021505376344}]

b = [{'mac': 'e200383d1149a4c975a59618', 'status': 'location_recording'}, {'mac': 'e200383d1149a90975a59629', 'status': 'location_environment'}]


expected output = [
{'site': 'KRM Plaza South Tower', 'building': 'd-block', 'level': 'reception', 'gw_mac': 'b827eb36fb0b_1', 'mac': 'e200383d1149a4c975a59618', 'rssi': -63.116279069767444, 'status': 'location_recording'}}, 
{'site': 'KRM Plaza South Tower', 'building': 'd-block', 'level': 'reception', 'gw_mac': 'b827eb36fb0b_1', 'mac': 'e200383d1149a90975a59629', 'rssi': -61.5, 'status': 'location_environment'}, 
{'site': 'KRM Plaza South Tower', 'building': 'd-block', 'level': 'reception', 'gw_mac': 'b827eb36fb0b_2', 'mac': 'e200383d1149a90975a59629', 'rssi': -59.086021505376344, 'status': 'location_environment'}]

有人可以帮助我实现预期的输出吗?

3 个答案:

答案 0 :(得分:1)

如果“ mac”键匹配,则可以遍历两个列表并合并字典。

这是一个使用列表理解的单行解决方案(假设列表与问题中的a和b相似)

In [6]: [{**a_val, **b_val} for a_val in a for b_val in b if b_val["mac"] == a_val["mac"]]
Out[6]:
[{'building': 'd-block',
  'gw_mac': 'b827eb36fb0b_1',
  'level': 'reception',
  'mac': 'e200383d1149a4c975a59618',
  'rssi': -63.116279069767444,
  'site': 'KRM Plaza South Tower',
  'status': 'location_recording'},
 {'building': 'd-block',
  'gw_mac': 'b827eb36fb0b_1',
  'level': 'reception',
  'mac': 'e200383d1149a90975a59629',
  'rssi': -61.5,
  'site': 'KRM Plaza South Tower',
  'status': 'location_environment'},
 {'building': 'd-block',
  'gw_mac': 'b827eb36fb0b_2',
  'level': 'reception',
  'mac': 'e200383d1149a90975a59629',
  'rssi': -59.086021505376344,
  'site': 'KRM Plaza South Tower',
  'status': 'location_environment'}]

注意:您可以使用Python 3.5上方的**执行字典合并,或者对字典下的版本使用字典的复制和更新功能。

答案 1 :(得分:1)

一种方法是使用循环命令。

例如:

a = [{'site': 'KRM Plaza South Tower', 'building': 'd-block', 'level': 'reception', 'gw_mac': 'b827eb36fb0b_1', 'mac': 'e200383d1149a4c975a59618', 'rssi': -63.116279069767444}, {'site': 'KRM Plaza South Tower', 'building': 'd-block', 'level': 'reception', 'gw_mac': 'b827eb36fb0b_1', 'mac': 'e200383d1149a90975a59629', 'rssi': -61.5}, {'site': 'KRM Plaza South Tower', 'building': 'd-block', 'level': 'reception', 'gw_mac': 'b827eb36fb0b_2', 'mac': 'e200383d1149a90975a59629', 'rssi': -59.086021505376344}]
b = [{'mac': 'e200383d1149a4c975a59618', 'status': 'location_recording'}, {'mac': 'e200383d1149a90975a59629', 'status': 'location_environment'}]

b = {i['mac']: i["status"] for i in b}  #loopup dict

for i in a:
    if i["mac"] in b:
        i.update({"status": b[i["mac"]]})
print(a)

输出:

[{'building': 'd-block',
  'gw_mac': 'b827eb36fb0b_1',
  'level': 'reception',
  'mac': 'e200383d1149a4c975a59618',
  'rssi': -63.116279069767444,
  'site': 'KRM Plaza South Tower',
  'status': 'location_recording'},
 {'building': 'd-block',
  'gw_mac': 'b827eb36fb0b_1',
  'level': 'reception',
  'mac': 'e200383d1149a90975a59629',
  'rssi': -61.5,
  'site': 'KRM Plaza South Tower',
  'status': 'location_environment'},
 {'building': 'd-block',
  'gw_mac': 'b827eb36fb0b_2',
  'level': 'reception',
  'mac': 'e200383d1149a90975a59629',
  'rssi': -59.086021505376344,
  'site': 'KRM Plaza South Tower',
  'status': 'location_environment'}]

答案 2 :(得分:1)

您可以使用以下简单的嵌套循环:

for item in b:
    for site in a:
        if site.get('mac') == item.get('mac'):
            site.update({'status':item.get('status')})

输出

[{'building': 'd-block',
  'gw_mac': 'b827eb36fb0b_1',
  'level': 'reception',
  'mac': 'e200383d1149a4c975a59618',
  'rssi': -63.116279069767444,
  'site': 'KRM Plaza South Tower',
  'status': 'location_recording'},
 {'building': 'd-block',
  'gw_mac': 'b827eb36fb0b_1',
  'level': 'reception',
  'mac': 'e200383d1149a90975a59629',
  'rssi': -61.5,
  'site': 'KRM Plaza South Tower',
  'status': 'location_environment'},
 {'building': 'd-block',
  'gw_mac': 'b827eb36fb0b_2',
  'level': 'reception',
  'mac': 'e200383d1149a90975a59629',
  'rssi': -59.086021505376344,
  'site': 'KRM Plaza South Tower',
  'status': 'location_environment'}]