Get: TypeError: 'dict_values' 对象在使用 python 3.2.3 时不支持索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17431638/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:11:31  来源:igfitidea点击:

Get: TypeError: 'dict_values' object does not support indexing when using python 3.2.3

pythonpython-3.x

提问by Jesse Pet

This is my code:

这是我的代码:

{names[i]:d.values()[i] for i in range(len(names))}

This works completely fine when using python 2.7.3; however, when I use python 3.2.3, I get an error stating 'dict_values' object does not support indexing. How can I modify the code to make it compatible for 3.2.3?

这在使用 python 2.7.3 时完全正常;但是,当我使用 python 3.2.3 时,我收到一条错误消息,指出'dict_values' object does not support indexing. 如何修改代码以使其兼容 3.2.3?

采纳答案by andersschuller

In Python 3, dict.values()(along with dict.keys()and dict.items()) returns a view, rather than a list. See the documentation here. You therefore need to wrap your call to dict.values()in a call to listlike so:

在 Python 3 中,dict.values()(连同dict.keys()dict.items())返回一个view,而不是一个列表。请参阅此处的文档。因此,您需要将您的呼叫包装dict.values()在这样的呼叫中list

v = list(d.values())
{names[i]:v[i] for i in range(len(names))}

回答by David Robinson

A simpler version of your code would be:

一个更简单的代码版本是:

dict(zip(names, d.values()))

If you want to keep the same structure, you can change it to:

如果要保持相同的结构,可以将其更改为:

vlst = list(d.values())
{names[i]: vlst[i] for i in range(len(names))}

(You can just as easily put list(d.values())inside the comprehension instead of vlst; it's just wasteful to do so since it would be re-generating the list every time).

(您可以轻松地将list(d.values())放入推导式而不是vlst;这样做很浪费,因为它每次都会重新生成列表)。

回答by martineau

In Python 3 the dict.values()method returns a dictionary view object, not a list like it does in Python 2. Dictionary views have a length, can be iterated, and support membership testing, but don't support indexing.

在 Python 3 中,该dict.values()方法返回一个字典视图对象,而不是像在 Python 2 中那样的列表。字典视图有长度,可以迭代,并支持成员资格测试,但不支持索引。

To make your code work in both versions, you could use either of these:

要使您的代码在两个版本中都能正常工作,您可以使用以下任一版本:

{names[i]:value for i,value in enumerate(d.values())}

    or

    或者

values = list(d.values())
{name:values[i] for i,name in enumerate(names)}

By far the simplest, fastest way to do the same thing in either version would be:

到目前为止,在任一版本中执行相同操作的最简单、最快的方法是:

dict(zip(names, d.values()))

Note however, that all of these methods will give you results that will vary depending on the actual contents of d. To overcome that, you may be able use an OrderedDictinstead, which remembers the order that keys were first inserted into it, so you can count on the order of what is returned by the values()method.

但是请注意,所有这些方法都会为您提供结果,具体取决于d. 为了克服这个问题,您可以改用OrderedDict,它会记住键第一次插入其中的顺序,因此您可以依靠该values()方法返回的顺序。