Python:如何将字典转换为可下标数组?

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

Python: how to convert a dictionary into a subscriptable array?

pythonpython-3.xdictionary

提问by quotable7

I know how to convert a dictionary into an list in Python, but somehow when I try to get, say, the sum of the resulting list, I get the error 'dict_values' object is not subscriptable. Also, I plan to sum only several items in the list.

我知道如何在 Python 中将字典转换为列表,但不知何故,当我尝试获取结果列表的总和时,我得到了错误'dict_values' object is not subscriptable。此外,我计划仅对列表中的几个项目进行求和。

dict = {A:1, B:2, C:3, D:4}
arr = dict.values()
the_sum = sum(arr[1:3])

Upon closer inspection, I noticed that when the resulting list is printed out, it always gives dict_values(......)as the output which I can't remove. How do I get around this?

仔细检查后,我注意到当结果列表被打印出来时,它总是dict_values(......)作为我无法删除的输出。我该如何解决这个问题?

采纳答案by Kasramvd

In python-3.X dict.valuesdoesn't return a listlike how it performs in python-2.X. In python-3.x it returns a dict-valueobject which is a set-like object and uses a hash table for storing its items. This feature, in addition to supporting most of setattributes, is very optimized for some operations like membership checking (using inoperator). And because of that, it doesn't support indexing.

在 python-3.Xdict.values中不返回list类似它在 python-2.X 中的执行方式。在 python-3.x 中,它返回一个dict-value对象,该对象是一个类似集合的对象,并使用哈希表来存储其项目。此功能除了支持大多数set属性外,还针对成员资格检查(使用in运算符)等某些操作进行了非常优化。正因为如此,它不支持索引。

If you want to get a list object, you need to convert it to list by passing the result to the list()function.

如果要获取列表对象,则需要通过将结果传递给list()函数来将其转换为列表。

the_values = dict.values()
SUM = sum(list(the_values)[1:10])

回答by txsaw1

Yes, it did appear like a list on the older Python questions asked here. But as @Kasramvd said, assuming you are using python 3.X, dict.valuesis a dictionary view object. (Also, you definitely came up with this example hastily as you have four dictionary entries but want 10 list items, which is redundant.)

是的,它确实看起来像是此处提出的较旧 Python 问题的列表。但正如@Kasramvd 所说,假设您使用的是 python 3.X,dict.values则是一个字典视图对象。(此外,您肯定是草率地想出了这个例子,因为您有四个字典条目但想要 10 个列表项,这是多余的。)

回答by Devansh Bansal

By assigning dict.values()to a list you are not converting it to a list; you are just storing it as dict_values (may be an object). To convert it write list=list(dict.values()). Now even while printing the listyou will get the list elements and not dict_values(......).

通过分配dict.values()给列表,您不会将其转换为列表;你只是将它存储为 dict_values (可能是一个对象)。要转换它写list=list(dict.values()). 现在,即使在打印时,list您也将获得列表元素而不是dict_values(......).

And as mentioned before don't use Python built-in names as your variable names; it may cause conflicts during execution and confusion while reading your code.

如前所述,不要使用 Python 内置名称作为变量名称;它可能会在执行过程中引起冲突,并在阅读代码时造成混乱。

回答by sharad jain

@mgilson is right. A dictionary , by its intrinsic nature, isn't ordered.

@mgilson 是对的。字典,就其内在性质而言,不是有序的。

Still you can do this :

你仍然可以这样做:

alpha = {"A":1,"B":2,"C":3,"D":4,"E":5}   # dictionary
my_list = []
for key,value in alpha.items() :
    my_list.append(value)

You can access your "values" from my_list, but it will not be in order.

您可以从 访问您的“值” my_list,但它不会按顺序进行。