【问题标题】:Python List of Dictionaries by LoopsPython 循环字典列表
【发布时间】:2017-12-04 12:23:12
【问题描述】:

我有 2 个 python 字典列表:

[
    {'index':'1','color':'red'},
    {'index':'2','color':'blue'},
    {'index':'3','color':'green'}
]

[
    {'device':'1','name':'x'},
    {'device':'2','name':'y'},
    {'device':'3','name':'z'}
]

如何将第二个列表中的每个字典附加到第一个列表中,以便获得如下输出:

[
    {'device':'1','name':'x','index': '1', 'color': 'red'},
    {'device':'1','name':'x','index': '2', 'color': 'blue'},
    {'device':'1','name': 'x','index': '3', 'color': 'green'}
    {'device':'2','name':'y''index': '1', 'color': 'red'},
    {'device':'2','name':'y','index': '2', 'color': 'blue'},
    {'device':'2','name':'y','index': '3', 'color': 'green'}
    {'device':'3','name':'z','index': '1', 'color': 'red'}, 
    {'device':'3','name':'z','index': '2', 'color': 'blue'}, 
    {'device':'3','name':'z','index': '3', 'color': 'green'}
]

【问题讨论】:

  • device 键在结果列表中的第 2 和第 3 个字典上去了哪里?
  • 为什么输入总共有6个字典,输出有9个?一些字典看起来已合并,但设备密钥仅在输出字典之一中?
  • 为什么输出是“列表列表”?
  • 在您的示例输出中,第一个列表的第一个字典是您想要的更新字典。但是,其他所有字典都只添加了“颜色”作为键。这是为什么?他们不应该都有“设备”作为键吗?
  • 是的,这是错误发生的,我更新了它。

标签: python list dictionary


【解决方案1】:

如果您只想将第一个列表的第 n 个字典与第二个列表的第 n 个字典合并,您可以执行以下操作:

a = [
    {'index':'1','color':'red'},
    {'index':'2','color':'blue'},
    {'index':'3','color':'green'}
]

b = [
    {'device':'1','name':'x'},
    {'device':'2','name':'y'},
    {'device':'3','name':'z'}
]

def merge(x,y):
    z = x.copy()
    z.update(y)
    return z

>>> [merge(x,y) for x,y in zip(a,b)]

[{'color': 'red', 'device': '1', 'index': '1', 'name': 'x'},
 {'color': 'blue', 'device': '2', 'index': '2', 'name': 'y'},
 {'color': 'green', 'device': '3', 'index': '3', 'name': 'z'}]

merge(x,y) 是一个辅助函数,它返回一个包含所有 x 和 y 内容的字典。

zip(a,b) 返回一个元组列表,其中每个元组的第一个元素是 a 的一个元素,第二个元素是 b 的一个元素。这允许在两个列表上并行使用列表推导。

【讨论】:

  • 你为什么要回答这个问题? OP 在 16 小时前发布了完全相同的问题(已回答)。回答明显的欺骗只会鼓励这种行为。
  • 其实不是同一个问题,输出不一样
【解决方案2】:

如果您只想打印结果字典,请取消注释打印语句(并注释以下 2)。

d1 = [
    {'index':'1','color':'red'},
    {'index':'2','color':'blue'},
    {'index':'3','color':'green'}
]

d2 = [
    {'device':'1','name':'x'},
    {'device':'2','name':'y'},
    {'device':'3','name':'z'}
]


result_list = []
for dict1 in d1:
    merged_dict = dict1.copy()
    for dict2 in d2:
        merged_dict.update(dict2)
#       print(merged_dict)
        result_list.append(merged_dict.copy())

print(result_list)

结果:

[{'name': 'x', 'device': '1', 'color': 'red', 'index': '1'},
{'name': 'y', 'device': '2', 'color': 'red', 'index': '1'},
{'name': 'z', 'device': '3', 'color': 'red', 'index': '1'},
{'name': 'x', 'device': '1', 'color': 'blue', 'index': '2'},
{'name': 'y', 'device': '2', 'color': 'blue', 'index': '2'},
{'name': 'z', 'device': '3', 'color': 'blue', 'index': '2'},
{'name': 'x', 'device': '1', 'color': 'green', 'index': '3'},
{'name': 'y', 'device': '2', 'color': 'green', 'index': '3'},
{'name': 'z', 'device': '3', 'color': 'green', 'index': '3'}]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-15
    • 1970-01-01
    • 2020-05-10
    • 2013-10-07
    • 2019-03-22
    • 2021-07-06
    • 1970-01-01
    相关资源
    最近更新 更多