简体   繁体   English

将不同的字典合并到一个列表中

[英]Merging different dictionaries together in one list

I have created 4 different sets of dictionaries with the guidance of this post: Python variables as keys to dict .在这篇文章的指导下,我创建了 4 组不同的字典: Python variables as keys to dict I now want to merge all these dictionaries into 1 list.我现在想将所有这些字典合并到 1 个列表中。 I tried the following:我尝试了以下方法:

classes = ['apple', 'orange', 'pear', 'mango']
class_dict = {}
store = []

for fruit in classes:
    if fruit == "orange":
        o = 2
        q = 1
    else:
        o = 0
        q = 0

    for j in ('fruit', 'o', 'q'):
        class_dict[j] = locals()[j]
    print (class_dict)
    store.append(class_dict)
print ("store: ", store)

The output is as shown below. output如下图所示。 As you can see, store only contains a list of the same dictionary being appended to it each time.如您所见, store仅包含每次附加到它的同一字典的列表。 I'm not sure where I'm going wrong and some help on this would be much appreciated!我不确定我要去哪里错了,对此提供一些帮助将不胜感激!

{'fruit': 'apple', 'o': 0, 'q': 0}
{'fruit': 'orange', 'o': 2, 'q': 1}
{'fruit': 'pear', 'o': 0, 'q': 0}
{'fruit': 'mango', 'o': 0, 'q': 0}

store:  [{'fruit': 'mango', 'o': 0, 'q': 0}, {'fruit': 'mango', 'o': 0, 'q': 0}, {'fruit': 'mango', 'o': 0, 'q': 0}, {'fruit': 'mango', 'o': 0, 'q': 0}]

You need to move your class_dict inside the loop.您需要在循环内移动您的class_dict

classes = ['apple', 'orange', 'pear', 'mango']

store = []

for fruit in classes:
    class_dict = {}
    if fruit == "orange":
        o = 2
        q = 1
    else:
        o = 0
        q = 0

    for j in ('fruit', 'o', 'q'):
        class_dict[j] = locals()[j]
    print (class_dict)
    store.append(class_dict)
print ("store: ", store)

output: output:

{'fruit': 'apple', 'o': 0, 'q': 0}
{'fruit': 'orange', 'o': 2, 'q': 1}
{'fruit': 'pear', 'o': 0, 'q': 0}
{'fruit': 'mango', 'o': 0, 'q': 0}
store:  [{'fruit': 'apple', 'o': 0, 'q': 0}, {'fruit': 'orange', 'o': 2, 'q': 1}, {'fruit': 'pear', 'o': 0, 'q': 0}, {'fruit': 'mango', 'o': 0, 'q': 0}]

You should just move class_dict inside the loop:您应该只在循环内移动class_dict

classes = ['apple', 'orange', 'pear', 'mango']

store = []

for fruit in classes:
    class_dict = {}
    if fruit == "orange":
        o = 2
        q = 1
    else:
        o = 0
        q = 0

    for j in ('fruit', 'o', 'q'):
        class_dict[j] = locals()[j]
    print (class_dict)
    store.append(class_dict)
print ("store: ", store)

It's because dict is a mutable object in python and in every iteration of your for loop you change the value of the global variable class_dict .这是因为dict是 python 中的可变 object ,并且在for循环的每次迭代中,您都会更改全局变量class_dict的值。 The simple example of it:它的简单示例:

>>> a = {'a': 1, 'b': 2}
>>> a
{'a': 1, 'b': 2}
>>> b = a
>>> b
{'a': 1, 'b': 2}
>>> b['c'] = 3
>>> b
{'a': 1, 'b': 2, 'c': 3}
>>> a
{'a': 1, 'b': 2, 'c': 3}

When you move class_dict inside the loop, this variable becomes local and iterations of the loop become independent.当您在循环内移动class_dict时,此变量变为本地变量,并且循环的迭代变得独立。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM