Python字典:keys()和values()总是相同的顺序吗?

本文翻译自:Python dictionary: are keys() and values() always the same order?

It looks like the lists returned by keys() and values() methods of a dictionary are always a 1-to-1 mapping (assuming the dictionary is not altered between calling the 2 methods). 看起来字典的keys()values()方法返回的列表始终是一对一的映射(假设字典在调用这两个方法之间没有改变)。

For example: 例如:

>>> d = {'one':1, 'two': 2, 'three': 3}
>>> k, v = d.keys(), d.values()
>>> for i in range(len(k)):
    print d[k[i]] == v[i]

True
True
True

If you do not alter the dictionary between calling keys() and calling values() , is it wrong to assume the above for-loop will always print True? 如果您不更改调用keys()和调用values()之间的字典,那么假设上述for循环将始终显示True是否错误? I could not find any documentation confirming this. 我找不到任何证明文件。


#1楼

参考:https://stackoom.com/question/3vfe/Python字典-keys-和values-总是相同的顺序吗


#2楼

I wasn't satisfied with these answers since I wanted to ensure the exported values had the same ordering even when using different dicts. 我对这些答案不满意,因为我想确保即使使用不同的字典,导出的值也具有相同的顺序。

Here you specify the key order upfront, the returned values will always have the same order even if the dict changes, or you use a different dict. 在这里,您可以预先指定键顺序,即使字典更改,返回的值也将始终具有相同的顺序,或者您使用其他字典。

keys = dict1.keys()
ordered_keys1 = [dict1[cur_key] for cur_key in keys]
ordered_keys2 = [dict2[cur_key] for cur_key in keys]

#3楼

Yes. 是。 Starting with CPython 3.6, dictionaries return items in the order you inserted them . 从CPython 3.6开始,字典按插入顺序返回项目

Ignore the part that says this is an implementation detail. 忽略说这是实现细节的部分。 This behaviour is guaranteed in CPython 3.6 and is required for all other Python implementations starting with Python 3.7 . 此行为在CPython 3.6中得到保证,并且从Python 3.7开始的所有其他Python实现都需要此行为。


#4楼

Good references to the docs. 对文档的良好引用。 Here's how you can guarantee the order regardless of the documentation / implementation: 不管文档/实现如何,都可以通过以下方法保证订单:

k, v = zip(*d.iteritems())

#5楼

Yes, what you observed is indeed a guaranteed property -- keys(), values() and items() return lists in congruent order if the dict is not altered. 是的,您观察到的确实是一个有保证的属性-如果未更改dict,则keys(),values()和items()会以一致的顺序返回列表。 iterkeys() &c also iterate in the same order as the corresponding lists. iterkeys()&c也以与相应列表相同的顺序进行迭代。


#6楼

According to http://docs.python.org/dev/py3k/library/stdtypes.html#dictionary-view-objects , the keys(), values() and items() methods of a dict will return corresponding iterators whose orders correspond. 根据http://docs.python.org/dev/py3k/library/stdtypes.html#dictionary-view-objects ,字典的keys(),values()和items()方法将返回其顺序的相应迭代器对应。 However, I am unable to find a reference to the official documentation for python 2.x for the same thing. 但是,对于同一件事,我找不到对python 2.x官方文档的引用。

So as far as I can tell, the answer is yes, but only in python 3.0+ 据我所知,答案是肯定的,但仅适用于python 3.0+

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值