将字典附加到循环 Python 中的列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23724136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:23:03  来源:igfitidea点击:

Appending a dictionary to a list in a a loop Python

pythonlistdictionaryappend

提问by Acoop

I am a basic python programmer so hopefully the answer to my question will be easy. I am trying to take a dictionary and append it to a list. The dictionary then changes values and then is appended again in a loop. It seems that every time I do this, all the dictionaries in the list change their values to match the one that was just appended. For example:

我是一个基本的 Python 程序员,所以希望我的问题的答案很简单。我正在尝试使用字典并将其附加到列表中。然后字典更改值,然后在循环中再次附加。似乎每次我这样做时,列表中的所有字典都会更改它们的值以匹配刚刚附加的值。例如:

>>> dict = {}
>>> list = []
>>> for x in range(0,100):
...     dict[1] = x
...     list.append(dict)
... 
>>> print list

I would assume the result would be [{1:1}, {1:2}, {1:3}... {1:98}, {1:99}]but instead I got:

我会假设结果是,[{1:1}, {1:2}, {1:3}... {1:98}, {1:99}]但我得到了:

[{1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}]

Any help is greatly appreciated.

任何帮助是极大的赞赏。

采纳答案by Martijn Pieters

You need to append a copy, otherwise you are just adding references to the same dictionary over and over again:

您需要附加一个副本,否则您只是一遍又一遍地添加对同一个字典的引用:

yourlist.append(yourdict.copy())

I used yourdictand yourlistinstead of dictand list; you don't want to mask the built-in types.

我用yourdictandyourlist而不是dictand list; 您不想屏蔽内置类型。

回答by Padraic Cunningham

You can also use zipand list comprehension to do what you need.

您还可以使用zip和列表理解来执行您需要的操作。

If you want the dict values to start at one use range(1,100)

如果您希望 dict 值从一次使用开始 range(1,100)

l = [dict(zip([1],[x])) for x in range(1,100)]

回答by chapelo

When you create the adictdictionary outside of the loop, you are appending the same dict to your alistlist. It means that all the copies point to the same dictionary and you are getting the last value {1:99}every time. Just create every dictionary inside the loop and now you have your 100 different dictionaries.

当您adict在循环之外创建字典时,您会将相同的字典附加到您的alist列表中。这意味着所有副本都指向同一个字典,并且{1:99}每次都获得最后一个值。只需在循环中创建每个字典,现在您就有了 100 个不同的字典。

alist = []
for x in range(100):
    adict = {1:x}
    alist.append(adict)
print(alist)

回答by docjag

Just put dict = {}inside the loop.

只需放入dict = {}循环内即可。

>>> dict = {}
>>> list = []
>>> for x in range(0, 100):
       dict[1] = x
       list.append(dict)
       dict = {}

>>> print list