【发布时间】:2017-08-23 05:44:30
【问题描述】:
我刚刚收到错误:
Traceback (most recent call last):
File "./download_documents.py", line 153, in <module>
paragraphs, used_pages = find_pages(lang, to_extract)
File "./download_documents.py", line 67, in find_pages
random.shuffle(page_titles_queue)
File "/usr/lib/python2.7/random.py", line 291, in shuffle
x[i], x[j] = x[j], x[i]
KeyError: 1
这让我很困惑。
-
random.shuffle似乎适用于零元素列表和一元素列表。 -
page_titles_queue是一个元组列表。 - 在
random.shuffle(page_titles_queue)之后的两行,有page_titles_queue.pop(),但这不应该影响随机播放。对吗?
那么 KeyError 的可能原因是什么?
我在 Ubuntu 16.04 上使用 Python 2.7.12。
【问题讨论】:
-
我现在让相同的代码再次运行。让我们看看会发生什么。
-
您可能对
page_titles_queue的实际含义有误。 -
你在字典上运行它:
random.shuffle(dict.fromkeys([1, 2, 3])). -
@AshwiniChaudhary 哎呀......你是对的。我应该删除问题吗?我想这可能对其他人有用。你想发表你的评论作为答案吗?
标签: python python-2.7 random shuffle keyerror