简体   繁体   English

有序 dict 是否也按顺序返回键?

[英]Does ordered dict return keys in order too?

For example:例如:

my_dict = OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)])

If an operation to get keys ie my_dict.keys() or my_dict.iterkeys() is done, is it guaranteed to return: ['apple','banana','orange','pear'] ?如果获取密钥的操作即my_dict.keys()my_dict.iterkeys()完成,是否保证返回: ['apple','banana','orange','pear']

This is not explicitly mentioned in the docs.这在文档中没有明确提到。

Yes, all iterations are guaranteed to use the same order.是的,所有迭代都保证使用相同的顺序。

If they didn't, that'd be a very big exception that would have to be explicitly documented.如果他们没有,那将是一个非常大的例外,必须明确记录。 The documentation contains no such exceptions.该文档不包含此类例外。

You can always check the implementation , explicitly linked from the top of the module documentation.您可以随时检查 实现,从模块文档的顶部明确链接。 You'll note that all method implementations use iteration over self , either directly or via iter(self) or list(self) ;您会注意到所有方法实现都直接或通过iter(self)list(self)self使用迭代; all these are fed by the OrderedDict.__iter__ method, which produces keys in order:所有这些都由OrderedDict.__iter__方法提供,该方法按顺序生成键:

def __iter__(self):
    'od.__iter__() <==> iter(od)'
    # Traverse the linked list in order.
    root = self.__root
    curr = root[1]                                  # start at the first node
    while curr is not root:
        yield curr[2]                               # yield the curr[KEY]
        curr = curr[1]                              # move to next node

For example, the OrderedDict.values() method uses those keys to map to values:例如, OrderedDict.values()方法使用这些键映射到值:

def values(self):
    'od.values() -> list of values in od'
    return [self[key] for key in self]

Yes, OrderedDict will return the keys in order.是的, OrderedDict将按顺序返回键。 It is worth mentioning, however, that the keys views compare order insensitive which is somewhat surprising.然而,值得一提的是,视图比较顺序不敏感,这有点令人惊讶。

Here is the 2.7 reference python implementation of __iter__ , which both of .keys and .iterkeys methods will delegate to:这是__iter__的 2.7参考 python 实现.keys.iterkeys方法都将委托给:

def __iter__(self):
    'od.__iter__() <==> iter(od)'
    # Traverse the linked list in order.
    root = self.__root
    curr = root[1]                                  # start at the first node
    while curr is not root:
        yield curr[2]                               # yield the curr[KEY]
        curr = curr[1]                              # move to next node

self.__root is just a list here. self.__root只是这里的一个列表。

Note: actual implementation is in C code.注意:实际实现是在 C 代码中。

Short answer: yes简短回答:是的

Longer answer:更长的答案:

the implementation of the keys (and also the values) method is based upon the __iter__ method which "Traverse the linked list in order."键(以及值)方法的实现基于__iter__方法,该方法“按顺序遍历链表”。

    def keys(self):
        return list(self)

    def values(self):
        return [self[key] for key in self]

You can check the source code linked in the documentation :您可以查看文档中链接的源代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM