我跑的代码的此块并得到类似下面的错误:奇怪的运行时错误而迭代的字典
Traceback (most recent call last):
File "urllister.py", line 26, in <module>
for k in l: print k,"points to",l[k],"\n"
RuntimeError: dictionary changed size during iteration
我唯一要做的是在在线路环路27
from sgmllib import SGMLParser
class URLLister(SGMLParser):
def reset(self):
SGMLParser.reset(self)
self.data = []
def start_a(self, attrs):
href = [v for k , v in attrs if k == 'href']
if href:
self.data.extend(href)
if __name__ == '__main__':
import urllib
sock = urllib.urlopen("http://diveintopython.org")
parser = URLLister()
html = sock.read()
parser.feed(html)
sock.close()
parser.close()
for url in parser.data: print url
l = locals()
for k in l:
print k,"points to",l[k],"\n"
在迭代它时,您无法更新或编辑字典。 –