I would've expected Python's keys method to return a set instead of a list. Since it most closely resembles the kind of guarantees that keys of a hashmap would give. Specifically, they are unique and not sorted, like a set. However, this method returns a list:
我希望Python的key方法返回一个集合而不是列表。因为它最类似于hashmap的键所提供的保证。具体来说,它们是唯一的,没有排序,就像一组。但是,此方法返回一个列表:
>>> d = {}
>>> d.keys().__class__
<type 'list'>
Is this just a mistake in the Python API or is there some other reason I am missing?
这只是Python API中的一个错误,还是我还缺少其他原因?
1 个解决方案
#1
62
One reason is that dict.keys()
predates the introduction of sets into the language.
一个原因是dict.keys()早于将集合引入语言。
Note that the return type of dict.keys()
has changed in Python 3: the function now returns a view rather than a list.
请注意,dict.keys()的返回类型在Python 3中已更改:该函数现在返回视图而不是列表。
#1
62
One reason is that dict.keys()
predates the introduction of sets into the language.
一个原因是dict.keys()早于将集合引入语言。
Note that the return type of dict.keys()
has changed in Python 3: the function now returns a view rather than a list.
请注意,dict.keys()的返回类型在Python 3中已更改:该函数现在返回视图而不是列表。