为什么不使用copy()python将dict附加到列表中

ici*_*ing 0 python dictionary loops

我正试图将一个字典附加到列表中.我在网站上已经看过几次这个问题,但是使用.copy()提出的解决方案似乎没有解决问题.我是否还需要循环我的附加变量?

for page in pages :
  if 'http' in page :
    machineList = []
    machine = getPinballMachine(page, siteURL) # this function returns a dict(ionary) of elements from a webpage
    machineList.append(machine.copy())
print (machineList)
Run Code Online (Sandbox Code Playgroud)

只打印最终字典.

Bre*_*arn 7

每次进行循环时,您都要将列表重置为空.将行machineList = []移到循环外(在之前for),以便在进入循环之前只创建一个列表.