创建字典列表会生成同一字典的副本列表 [英] Creating a list of dictionaries results in a list of copies of the same dictionary

查看:22
本文介绍了创建字典列表会生成同一字典的副本列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从网页中获取所有 iframe.

I want to get all the iframe from a webpage.

代码:

site = "http://" + url
f = urllib2.urlopen(site)
web_content =  f.read()

soup = BeautifulSoup(web_content)
info = {}
content = []
for iframe in soup.find_all('iframe'):
    info['src'] = iframe.get('src')
    info['height'] = iframe.get('height')
    info['width'] = iframe.get('width')
    content.append(info)
    print(info)       

pprint(content)

print(info) 的结果:

{'src': u'abc.com', 'width': u'0', 'height': u'0'}
{'src': u'xyz.com', 'width': u'0', 'height': u'0'}
{'src': u'http://www.detik.com', 'width': u'1000', 'height': u'600'}

pprint(content) 的结果:

[{'height': u'600', 'src': u'http://www.detik.com', 'width': u'1000'},
{'height': u'600', 'src': u'http://www.detik.com', 'width': u'1000'},
{'height': u'600', 'src': u'http://www.detik.com', 'width': u'1000'}]

为什么内容的值不对?假设它与我 print(info) 时的值相同.

Why is the value of the content not right? It's suppose to be the same as the value when I print(info).

推荐答案

你不是为每个 iframe 创建一个单独的字典,你只是一遍又一遍地修改同一个字典,并不断在你的清单.

You are not creating a separate dictionary for each iframe, you just keep modifying the same dictionary over and over, and you keep adding additional references to that dictionary in your list.

请记住,当您执行诸如 content.append(info) 之类的操作时,您并不是在制作数据的副本,您只是在添加对数据的引用.

Remember, when you do something like content.append(info), you aren't making a copy of the data, you are simply appending a reference to the data.

您需要为每个 iframe 创建一个新字典.

You need to create a new dictionary for each iframe.

for iframe in soup.find_all('iframe'):
    info = {}
    ...

更好的是,您不需要先创建一个空字典.只需一次创建即可:

Even better, you don't need to create an empty dictionary first. Just create it all at once:

for iframe in soup.find_all('iframe'):
    info = {
        "src": iframe.get('src'),
        "height": iframe.get('height'),
        "width": iframe.get('width'),
    }
    content.append(info)

还有其他方法可以实现这一点,例如迭代属性列表,或者使用列表或字典推导式,但很难提高上述代码的清晰度.

There are other ways to accomplish this, such as iterating over a list of attributes, or using list or dictionary comprehensions, but it's hard to improve upon the clarity of the above code.

这篇关于创建字典列表会生成同一字典的副本列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆