【问题标题】:How to insert from two lists into a list of dictionaries with identical keynames in Python 3.6.1如何从两个列表插入到 Python 3.6.1 中具有相同键名的字典列表中
【发布时间】:2017-11-15 16:06:22
【问题描述】:

我是 Python 和字典的新手。使用 Python 3.6.1。我搜索了类似的问题,但其他人使用的解决方案似乎涉及具有不同键的字典,并且许多答案与旧版本的 Python 有关。任何帮助表示赞赏!

我有以下在打印时返回的字典列表:

[{'Coordinate': array([None, None], dtype=object), 'Height': None}, 
{'Coordinate': array([None, None], dtype=object), 'Height': None}, 
{'Coordinate': array([None, None], dtype=object), 'Height': None}, 
{'Coordinate': array([None, None], dtype=object), 'Height': None}, 
{'Coordinate': array([None, None], dtype=object), 'Height': None}, 
{'Coordinate': array([None, None], dtype=object), 'Height': None}, 
{'Coordinate': array([None, None], dtype=object), 'Height': None}, 
{'Coordinate': array([None, None], dtype=object), 'Height': None}, 
{'Coordinate': array([None, None], dtype=object), 'Height': None}, 
{'Coordinate': array([None, None], dtype=object), 'Height': None}]

我想将以下列表中的值插入到上述字典列表中:

coordkeys = ['Coordinate', 'Coordinate', 'Coordinate', 'Coordinate', 'Coordinate', 'Coordinate', 'Coordinate', 'Coordinate', 'Coordinate', 'Coordinate']

coordvalues = [[44,33], [55,22], [77,66], [88,99], [77,11], [46,78], [44,33], [13,92], [21,69], [79,91]]

heightkeys = ['Height', 'Height', 'Height', 'Height', 'Height', 'Height', 'Height', 'Height', 'Height', 'Height']

heightvalues = [333, 444, 555, 666, 777, 888, 999, 222, 2222, 3333]

我想从这些列表中将值插入到字典列表中。因此结果遵循以下模式: 第一个 valuelist 的第一个条目将对应于第二个 valuelist 的第一个值(两者都在同一个字典中)。 显然,如果可能的话,我不希望使用 coordkeys 和 heightkeys 列表。

为了说明最终结果的模式,使用两个列表中的所有值,最终结果开始如下:

[{'Coordinate': [44,33], 'Height': 333}, {'Coordinate': [55,22], 'Height': 444}, # And so on until the end of both lists ]

我尝试只使用一个列表,如下所示:

geographic_details = dict(list(zip(coordkeys, coordvalues)))

但是输出的打印只返回坐标值列表中的最后一个条目,而不是列表中的所有值:

{'Coordinate': [79, 91]}

所以显然只保留了最后一个键值对,因为有几个同名的键。 zip 或这种使用 zip 的方式似乎不是这里的方式。

编辑:

我尝试通过这样做从两个列表中添加,但似乎不允许这样做。:

最后一行代码遗漏了一个结束括号。现在我添加了它,它看起来像这样:

geographic_details = dict(list(zip(coordkeys, coordvalues), zip(heightkeys, heightvalues)))

但是返回一个新的错误:

TypeError: list() takes at most 1 argument (2 given)

【问题讨论】:

  • geographic_details = dict(list(zip(coordkeys, coordvalues), zip(heightkeys, heightvalues)))
  • 你在末尾缺少一个右括号。
  • 感谢您指出这一点,现在已修复。现在出现新错误,请参阅编辑。
  • 请不要在一个问题中逐步询问代码中的每个问题。在每篇文章中提出一个具体问题。

标签: python list dictionary


【解决方案1】:

你想要这样的东西吗?

import numpy as np
numpy_array=[{'Coordinate': np.array([None, None], dtype=object), 'Height': None},
{'Coordinate': np.array([None, None], dtype=object), 'Height': None},
{'Coordinate': np.array([None, None], dtype=object), 'Height': None},
{'Coordinate': np.array([None, None], dtype=object), 'Height': None},
{'Coordinate': np.array([None, None], dtype=object), 'Height': None},
{'Coordinate': np.array([None, None], dtype=object), 'Height': None},
{'Coordinate': np.array([None, None], dtype=object), 'Height': None},
{'Coordinate': np.array([None, None], dtype=object), 'Height': None},
{'Coordinate': np.array([None, None], dtype=object), 'Height': None},
{'Coordinate': np.array([None, None], dtype=object), 'Height': None}]


coordkeys = ['Coordinate', 'Coordinate', 'Coordinate', 'Coordinate', 'Coordinate', 'Coordinate', 'Coordinate', 'Coordinate', 'Coordinate', 'Coordinate']

coordvalues = [[44,33], [55,22], [77,66], [88,99], [77,11], [46,78], [44,33], [13,92], [21,69], [79,91]]

heightkeys = ['Height', 'Height', 'Height', 'Height', 'Height', 'Height', 'Height', 'Height', 'Height', 'Height']

heightvalues = [333, 444, 555, 666, 777, 888, 999, 222, 2222, 3333]

for index,value in enumerate(zip(coordvalues,heightvalues)):
    for index_1,value_1 in enumerate(numpy_array):
        for key,value_2 in value_1.items():
            if index==index_1:
                if isinstance(value_2,np.ndarray):
                    value_2[0]=value[0][0]
                    value_2[1]=value[0][1]
                value_1['Height']=value[1]

print(numpy_array)

输出:

[{'Height': 333, 'Coordinate': array([44, 33], dtype=object)}, {'Height': 444, 'Coordinate': array([55, 22], dtype=object)}, {'Height': 555, 'Coordinate': array([77, 66], dtype=object)}, {'Height': 666, 'Coordinate': array([88, 99], dtype=object)}, {'Height': 777, 'Coordinate': array([77, 11], dtype=object)}, {'Height': 888, 'Coordinate': array([46, 78], dtype=object)}, {'Height': 999, 'Coordinate': array([44, 33], dtype=object)}, {'Height': 222, 'Coordinate': array([13, 92], dtype=object)}, {'Height': 2222, 'Coordinate': array([21, 69], dtype=object)}, {'Height': 3333, 'Coordinate': array([79, 91], dtype=object)}]

【讨论】:

    【解决方案2】:

    鉴于列表大小相等,map 函数看起来是创建所需数据结构的最优雅方式。

    coordkeys = ['Coordinate', 'Coordinate', 'Coordinate', 'Coordinate', 'Coordinate', 'Coordinate', 'Coordinate', 'Coordinate', 'Coordinate', 'Coordinate']
    coordvalues = [[44,33], [55,22], [77,66], [88,99], [77,11], [46,78], [44,33], [13,92], [21,69], [79,91]]
    heightkeys = ['Height', 'Height', 'Height', 'Height', 'Height', 'Height', 'Height', 'Height', 'Height', 'Height']
    heightvalues = [333, 444, 555, 666, 777, 888, 999, 222, 2222, 3333]
    
    
    def convert(c, cval, h, hval):
        return {c:cval, h:hval}
    
    assert list(map(convert, coordkeys, coordvalues, heightkeys, heightvalues)) == [{'Coordinate': [44, 33], 'Height': 333}, 
        {'Coordinate': [55, 22], 'Height': 444}, 
        {'Coordinate': [77, 66], 'Height': 555},
        {'Coordinate': [88, 99], 'Height': 666},
        {'Coordinate': [77, 11], 'Height': 777}, 
        {'Coordinate': [46, 78], 'Height': 888}, 
        {'Coordinate': [44, 33], 'Height': 999}, 
        {'Coordinate': [13, 92], 'Height': 222}, 
        {'Coordinate': [21, 69], 'Height': 2222}, 
        {'Coordinate': [79, 91], 'Height': 3333}]
    

    【讨论】:

      猜你喜欢
      • 2021-11-26
      • 1970-01-01
      • 2019-11-04
      • 2017-04-20
      • 1970-01-01
      • 2019-12-26
      • 2018-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多