使用 for 循环填充 Python 字典列表

时间:2021-04-11 03:47:28

标签: python list dictionary for-loop

我正在尝试使用 for 循环填充字典列表,但最终结果显示 for 循环填充的最后一个字典覆盖了所有先前字典的值。我已尝试将 How to populate Python dictionary using a loop 中提出的解决方案调整到我的代码中,但我仍然得到相同的结果。

我过去曾将 .append() 与列表对象一起使用,但我不确定为什么每次迭代时都会覆盖字典。这是我试图实现的可重现示例:

my_list1 = [{'x1':1, 'x2':2.4, 'x3':3}, {'x1':4.5, 'x2':5, 'x3':6}, {'x1':7.9, 'x2':8.3, 'x3':9},  {'x1':3, 'x2':0.3, 'x3':4}]
list_test = []
foo = {}

for i in range(4):
    for j in range(3):

        if isinstance(list(my_list1[i].values())[j], float) and  list(my_list1[i].values())[j] > 0.1:    
            foo[list(my_list1[i].keys())[j]] = [float(x) for x in np.linspace(start = list(my_list1[i].values())[j] - 0.5,  stop = list(my_list1[i].values())[j] + 0.5, num = 3)]
    
        elif isinstance(list(my_list1[i].values())[j], int) and  list(my_list1[i].values())[j] > 0.1:    
            foo[list(my_list1[i].keys())[j]] = [float(x) for x in np.linspace(start = list(my_list1[i].values())[j] - 0.5,  stop = list(my_list1[i].values())[j] + 0.5, num = 3)]
           
    list_test.append(foo)

运行上述代码时list_test的实际输出为:

[{'x1': [2.5, 3.0, 3.5], 'x2': [-0.2, 0.3, 0.8], 'x3': [3.5, 4.0, 4.5]},
 {'x1': [2.5, 3.0, 3.5], 'x2': [-0.2, 0.3, 0.8], 'x3': [3.5, 4.0, 4.5]},
 {'x1': [2.5, 3.0, 3.5], 'x2': [-0.2, 0.3, 0.8], 'x3': [3.5, 4.0, 4.5]},
 {'x1': [2.5, 3.0, 3.5], 'x2': [-0.2, 0.3, 0.8], 'x3': [3.5, 4.0, 4.5]}]

但我期待看到:

[{'x1': [0.5, 1.0, 1.5], 'x2': [1.9, 2.4, 2.9], 'x3': [2.5, 3.0, 3.5]},
 {'x1': [4.0, 4.5, 5.0], 'x2': [4.5, 5.0, 5.5], 'x3': [5.5, 6.0, 6.5]},
 {'x1': [7.4, 7.9, 8.4], 'x2': [7.8, 8.3, 8.8], 'x3': [8.5, 9.0, 9.5]},
 {'x1': [2.5, 3.0, 3.5], 'x2': [-0.2, 0.3, 0.8], 'x3': [3.5, 4.0, 4.5]}]

任何实现解决方案的建议将不胜感激。

2 个答案:

答案 0 :(得分:1)

由于 foo 在您的代码(字典)中是可变类型,因此在每一行中您对其进行的所有修改都对同一个字典进行。

所以如果我简单地说你的代码是做什么的

例如。

list1 = []

foo = {'a':1,'b':2}

list1.append(foo)

# list1 now is [foo]

foo['b'] = 3

list1.append(foo)

# list1 now is [foo, foo]
# i.e. [{'a':1,'b':3}, {'a':1,'b':3}]

你的问题的解决方案,在每一行修改的开始初始化一个新的字典foo:

my_list1 = [{'x1':1, 'x2':2.4, 'x3':3}, {'x1':4.5, 'x2':5, 'x3':6}, {'x1':7.9, 'x2':8.3, 'x3':9},  {'x1':3, 'x2':0.3, 'x3':4}]
list_test = []

for i in range(4):
    foo = {}
    for j in range(3):

        if isinstance(list(my_list1[i].values())[j], float) and  list(my_list1[i].values())[j] > 0.1:    
            foo[list(my_list1[i].keys())[j]] = [float(x) for x in np.linspace(start = list(my_list1[i].values())[j] - 0.5,  stop = list(my_list1[i].values())[j] + 0.5, num = 3)]
    
        elif isinstance(list(my_list1[i].values())[j], int) and  list(my_list1[i].values())[j] > 0.1:    
            foo[list(my_list1[i].keys())[j]] = [float(x) for x in np.linspace(start = list(my_list1[i].values())[j] - 0.5,  stop = list(my_list1[i].values())[j] + 0.5, num = 3)]
           
    list_test.append(foo)

输出:

>>> list_test

[{'x1': [0.5, 1.0, 1.5], 'x2': [1.9, 2.4, 2.9], 'x3': [2.5, 3.0, 3.5]}, 
{'x1': [4.0, 4.5, 5.0], 'x2': [4.5, 5.0, 5.5], 'x3': [5.5, 6.0, 6.5]}, 
{'x1': [7.4, 7.9, 8.4], 'x2': [7.8, 8.3, 8.8], 'x3': [8.5, 9.0, 9.5]}, 
{'x1': [2.5, 3.0, 3.5], 'x2': [-0.2, 0.3, 0.8], 'x3': [3.5, 4.0, 4.5]}]

答案 1 :(得分:1)

只需将此 foo = {} 放在行 for i in range(4):

之后