社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

为什么一个python数组只在元素被更改时向另一个数组追加一个指针?[复制品]

Jesse Reza Khorasanee • 5 年前 • 1535 次点击  

我想要所有的 iframe 从网页上。

代码:

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'}]

为什么内容的价值不对?当我 打印(信息) .

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40815
 
1535 次点击  
文章 [ 4 ]  |  最新文章 5 年前
fragilewindows XU Bin
Reply   •   1 楼
fragilewindows XU Bin    8 年前

如果需要一行:

list_of_dict = [{} for i in range(list_len)]
zenpoy
Reply   •   2 楼
zenpoy    13 年前

info 是一个指向字典的指针-您一直在向列表中添加相同的指针 contact .

插入 info = {} 它应该能解决这个问题:

...
content = []
for iframe in soup.find_all('iframe'):
    info = {}
    info['src'] = iframe.get('src')
    info['height'] = iframe.get('height')
    info['width'] = iframe.get('width')
...
fragilewindows XU Bin
Reply   •   3 楼
fragilewindows XU Bin    8 年前

你误解了巨蟒 list 对象。它类似于C pointer-array . 它实际上并不“复制”您附加到它的对象。相反,它只是存储一个指向该对象的“指针”。

请尝试以下代码:

>>> d={}
>>> dlist=[]
>>> for i in xrange(0,3):
    d['data']=i
    dlist.append(d)
    print(d)

{'data': 0}
{'data': 1}
{'data': 2}
>>> print(dlist)
[{'data': 2}, {'data': 2}, {'data': 2}]

那么为什么呢? print(dlist) 不一样 print(d) ?

下面的代码显示了原因:

>>> for i in dlist:
    print "the list item point to object:", id(i)

the list item point to object: 47472232
the list item point to object: 47472232
the list item point to object: 47472232

所以你可以看到 dlist 实际上是指向同一个 dict 对象。

这个问题的真正答案是通过使用 d.copy() .

>>> dlist=[]
>>> for i in xrange(0,3):
    d['data']=i
    dlist.append(d.copy())
    print(d)

{'data': 0}
{'data': 1}
{'data': 2}
>>> print dlist
[{'data': 0}, {'data': 1}, {'data': 2}]

试试 id() 技巧,你可以看到列表项实际上指向完全不同的对象。

>>> for i in dlist:
    print "the list item points to object:", id(i)

the list item points to object: 33861576
the list item points to object: 47472520
the list item points to object: 47458120
Bryan Oakley
Reply   •   4 楼
Bryan Oakley    13 年前

您并不是为每个iframe创建一个单独的字典,您只是不断地修改同一个字典,并不断地在列表中添加对该字典的其他引用。

记住,当你做 content.append(info) ,不是复制数据,只是将引用附加到数据。

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

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

更好的是,你不需要先创建一个空字典。一次全部创建:

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

有其他方法可以实现这一点,例如遍历属性列表,或者使用列表或字典理解,但是很难提高上述代码的清晰度。