[英]Appending a dictionary to a list in a loop
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:例如:
>>> 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}]
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 yourdict
and yourlist
instead of dict
and list
;我使用
yourdict
和yourlist
而不是dict
和list
; you don't want to mask the built-in types.您不想屏蔽内置类型。
When you create the adict
dictionary outside of the loop, you are appending the same dict to your alist
list.当您在循环之外创建
adict
字典时,您将相同的 dict 附加到您的alist
列表中。 It means that all the copies point to the same dictionary and you are getting the last value {1:99}
every time.这意味着所有副本都指向同一个字典,并且每次都获得最后一个值
{1:99}
。 Just create every dictionary inside the loop and now you have your 100 different dictionaries.只需在循环中创建每个字典,现在您就有了 100 个不同的字典。
alist = []
for x in range(100):
adict = {1:x}
alist.append(adict)
print(alist)
Just put dict = {}
inside the loop.只需将
dict = {}
放入循环中。
>>> dict = {}
>>> list = []
>>> for x in range(0, 100):
dict[1] = x
list.append(dict)
dict = {}
>>> print list
You can also use zip
and 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)]
Let's say d
is your dictionary.假设
d
是您的字典。 Here, if you do d.copy()
.在这里,如果您执行
d.copy()
。 It returns shallow copy that doesn't work when you have nested dictionary into d
dictionary.当您将字典嵌套到
d
字典中时,它返回不起作用的浅拷贝。 To overcome from this issue, we have to use deepcopy .为了克服这个问题,我们必须使用deepcopy 。
from copy import deepcopy
list.append(deepcopy(d))
It works perfectly !!!它完美地工作!!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.