Python 类型错误:'dict_keys' 对象不支持索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17322668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: 'dict_keys' object does not support indexing
提问by gate_007
def shuffle(self, x, random=None, int=int):
"""x, random=random.random -> shuffle list x in place; return None.
Optional arg random is a 0-argument function returning a random
float in [0.0, 1.0); by default, the standard random.random.
"""
randbelow = self._randbelow
for i in reversed(range(1, len(x))):
# pick an element in x[:i+1] with which to exchange x[i]
j = randbelow(i+1) if random is None else int(random() * (i+1))
x[i], x[j] = x[j], x[i]
When I run the shuffle
function it raises the following error, why is that?
当我运行该shuffle
函数时,它会引发以下错误,这是为什么?
TypeError: 'dict_keys' object does not support indexing
采纳答案by mgilson
Clearly you're passing in d.keys()
to your shuffle
function. Probably this was written with python2.x (when d.keys()
returned a list). With python3.x, d.keys()
returns a dict_keys
object which behaves a lot more like a set
than a list
. As such, it can't be indexed.
显然,您正在传递d.keys()
给您的shuffle
函数。可能这是用 python2.x 编写的(d.keys()
返回列表时)。使用 python3.x,d.keys()
返回一个dict_keys
更像 a 而set
不是 a 的对象list
。因此,它不能被索引。
The solution is to pass list(d.keys())
(or simply list(d)
) to shuffle
.
解决方案是将list(d.keys())
(或简单地list(d)
)传递给shuffle
.
回答by user4815162342
You're passing the result of somedict.keys()
to the function. In Python 3, dict.keys
doesn't return a list, but a set-like object that represents a view of the dictionary's keys and (being set-like) doesn't support indexing.
您正在将 的结果传递somedict.keys()
给函数。在 Python 3 中,dict.keys
不返回列表,而是一个类似集合的对象,它表示字典键的视图并且(类似集合)不支持索引。
To fix the problem, use list(somedict.keys())
to collect the keys, and work with that.
要解决问题,请使用list(somedict.keys())
收集密钥,然后使用它。
回答by FooBar167
Why you need to implement shuffle when it already exists? Stay on the shoulders of giants.
为什么在已经存在的情况下需要实现 shuffle?站在巨人的肩膀上。
import random
d1 = {0:'zero', 1:'one', 2:'two', 3:'three', 4:'four',
5:'five', 6:'six', 7:'seven', 8:'eight', 9:'nine'}
keys = list(d1)
random.shuffle(keys)
d2 = {}
for key in keys: d2[key] = d1[key]
print(d1)
print(d2)
回答by sahama
Convert an iterable to a list may have a cost. Instead, to get the the first item, you can use:
将可迭代对象转换为列表可能会产生成本。相反,要获取第一项,您可以使用:
next(iter(keys))
Or, if you want to iterate over all items, you can use:
或者,如果要遍历所有项目,可以使用:
items = iter(keys)
while True:
try:
item = next(items)
except StopIteration as e:
pass # finish
回答by DeWil
In Python 2 dict.keys() return a list, whereas in Python 3 it returns a generator.
在 Python 2 中 dict.keys() 返回一个列表,而在 Python 3 中它返回一个生成器。
You could only iterate over it's values else you may have to explicitly convert it to a list i.e. pass it to a list function.
您只能迭代它的值,否则您可能必须将其显式转换为列表,即将其传递给列表函数。