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).
它看起来像是由键()返回的列表,而字典的values()方法总是1到1的映射(假设字典在调用2方法之间没有改变)。
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? I could not find any documentation confirming this.
如果在调用keys()和调用values()之间不修改字典,那么假设上面的for循环将始终是正确的,这是错误的吗?我找不到任何证实这一点的文件。
7 个解决方案
#1
245
Found this:
发现了这个:
If
items()
,keys()
,values()
,iteritems()
,iterkeys()
, anditervalues()
are called with no intervening modifications to the dictionary, the lists will directly correspond.如果条目()、keys()、values()、iteritems()、iterkeys()和itervalues()被调用,而不需要对字典进行插入修改,那么列表将直接对应。
On 2.x documentation and 3.x documentation.
在2。x文档和3。x文档。
#2
68
Yes, what you observed is indeed a guaranteed property -- keys(), values() and items() return lists in congruent order if the dict is not altered. iterkeys() &c also iterate in the same order as the corresponding lists.
是的,您所观察到的确实是一个有保证的属性——keys()、values()和items()返回列表,如果该命令没有被改变的话,它们的顺序是一致的。iterkeys() &c也以对应的列表的顺序迭代。
#3
42
Yes it is guaranteed in python 2.x:
是的,它在python 2.x中得到保证:
If keys, values and items views are iterated over with no intervening modifications to the dictionary, the order of items will directly correspond.
如果对键、值和项视图进行迭代,而没有对字典进行插入修改,则项目的顺序将直接对应。
#4
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. However, I am unable to find a reference to the official documentation for python 2.x for the same thing.
根据http://docs.python.org/dev/py3k/library/stdtypes.html#dictionary-view-objects, keys(), values()和items()方法将返回对应的迭代器,它们的顺序对应。但是,我无法找到关于python 2官方文档的引用。x是一样的。
So as far as I can tell, the answer is yes, but only in python 3.0+
就我所知,答案是肯定的,但仅限于python 3.0+。
#5
6
For what it's worth, some heavy used production code I have written is based on this assumption and I never had a problem with it. I know that doesn't make it true though :-)
对于它的价值,我所写的一些大量使用的生产代码是基于这个假设的,而且我从来没有遇到过问题。我知道这并不能使它成真:-)
If you don't want to take the risk I would use iteritems() if you can.
如果您不想冒这个风险,我将使用iteritems(),如果可以的话。
for key, value in myDictionary.iteritems():
print key, value
#6
0
Yes. Starting with CPython 3.6, dictionaries return items in the order you inserted them.
是的。从CPython 3.6开始,字典会按照您插入的顺序返回项目。
The documentation hasn't been updated yet, ignore the part that says this is an implementation detail. This is already guaranteed in CPython 3.6 and will be required for all other Python implementations starting with Python 3.7.
文档还没有更新,忽略说这是实现细节的部分。这已经在CPython 3.6中得到了保证,并且将需要从Python 3.7开始的所有其他Python实现。
#7
-1
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]
#1
245
Found this:
发现了这个:
If
items()
,keys()
,values()
,iteritems()
,iterkeys()
, anditervalues()
are called with no intervening modifications to the dictionary, the lists will directly correspond.如果条目()、keys()、values()、iteritems()、iterkeys()和itervalues()被调用,而不需要对字典进行插入修改,那么列表将直接对应。
On 2.x documentation and 3.x documentation.
在2。x文档和3。x文档。
#2
68
Yes, what you observed is indeed a guaranteed property -- keys(), values() and items() return lists in congruent order if the dict is not altered. iterkeys() &c also iterate in the same order as the corresponding lists.
是的,您所观察到的确实是一个有保证的属性——keys()、values()和items()返回列表,如果该命令没有被改变的话,它们的顺序是一致的。iterkeys() &c也以对应的列表的顺序迭代。
#3
42
Yes it is guaranteed in python 2.x:
是的,它在python 2.x中得到保证:
If keys, values and items views are iterated over with no intervening modifications to the dictionary, the order of items will directly correspond.
如果对键、值和项视图进行迭代,而没有对字典进行插入修改,则项目的顺序将直接对应。
#4
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. However, I am unable to find a reference to the official documentation for python 2.x for the same thing.
根据http://docs.python.org/dev/py3k/library/stdtypes.html#dictionary-view-objects, keys(), values()和items()方法将返回对应的迭代器,它们的顺序对应。但是,我无法找到关于python 2官方文档的引用。x是一样的。
So as far as I can tell, the answer is yes, but only in python 3.0+
就我所知,答案是肯定的,但仅限于python 3.0+。
#5
6
For what it's worth, some heavy used production code I have written is based on this assumption and I never had a problem with it. I know that doesn't make it true though :-)
对于它的价值,我所写的一些大量使用的生产代码是基于这个假设的,而且我从来没有遇到过问题。我知道这并不能使它成真:-)
If you don't want to take the risk I would use iteritems() if you can.
如果您不想冒这个风险,我将使用iteritems(),如果可以的话。
for key, value in myDictionary.iteritems():
print key, value
#6
0
Yes. Starting with CPython 3.6, dictionaries return items in the order you inserted them.
是的。从CPython 3.6开始,字典会按照您插入的顺序返回项目。
The documentation hasn't been updated yet, ignore the part that says this is an implementation detail. This is already guaranteed in CPython 3.6 and will be required for all other Python implementations starting with Python 3.7.
文档还没有更新,忽略说这是实现细节的部分。这已经在CPython 3.6中得到了保证,并且将需要从Python 3.7开始的所有其他Python实现。
#7
-1
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]