random.shuffle 怎么会导致 KeyError?
How can random.shuffle result in a KeyError?
我刚收到错误:
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
random.shuffle
只是交换物品,发生异常的那一行非常清楚:
x[i], x[j] = x[j], x[i]
其中 x
是传入的 "sequence"。在这种情况下,i
和 j
将是 range(0, len(x))
范围内的值,如果i
或 j
中的任何一个不存在于 "sequence" 中,它将抛出 Exception
。在您的情况下,很可能会抛出 KeyError
:
>>> import random
>>> d = {i: i for i in range(7, 10)}
>>> random.shuffle(d)
KeyError: 3
然而,如果字典恰好包含构成 range(0, len(x))
:
的键,它会通过交换值来工作
>>> d = {i: i for i in range(10)}
>>> random.shuffle(d)
>>> d
{0: 7, 1: 9, 2: 3, 3: 4, 4: 0, 5: 2, 6: 1, 7: 6, 8: 8, 9: 5}
如果缺少一个或多个键,它可能会工作,或者可能会抛出 Exception
。这取决于将抽取哪些随机数:
d = {i: i for i in range(1, 10)}
random.shuffle(d) # works sometimes, but sometimes it throws the KeyError
我刚收到错误:
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
random.shuffle
只是交换物品,发生异常的那一行非常清楚:
x[i], x[j] = x[j], x[i]
其中 x
是传入的 "sequence"。在这种情况下,i
和 j
将是 range(0, len(x))
范围内的值,如果i
或 j
中的任何一个不存在于 "sequence" 中,它将抛出 Exception
。在您的情况下,很可能会抛出 KeyError
:
>>> import random
>>> d = {i: i for i in range(7, 10)}
>>> random.shuffle(d)
KeyError: 3
然而,如果字典恰好包含构成 range(0, len(x))
:
>>> d = {i: i for i in range(10)}
>>> random.shuffle(d)
>>> d
{0: 7, 1: 9, 2: 3, 3: 4, 4: 0, 5: 2, 6: 1, 7: 6, 8: 8, 9: 5}
如果缺少一个或多个键,它可能会工作,或者可能会抛出 Exception
。这取决于将抽取哪些随机数:
d = {i: i for i in range(1, 10)}
random.shuffle(d) # works sometimes, but sometimes it throws the KeyError