gpt4 book ai didi

Python 字典 : are keys() and values() always the same order?

转载 作者:IT老高 更新时间:2023-10-28 12:09:07 24 4
gpt4 key购买 nike

看起来字典的 keys()values() 方法返回的列表总是一对一的映射(假设字典不是在调用 2 个方法之间进行了更改)。

例如:

>>> 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

如果你在调用 keys() 和调用 values() 之间不改变字典,假设上面的 for 循环总是打印 True 是错误的吗?我找不到任何文件证实这一点。

最佳答案

找到这个:

If items(), keys(), values(), iteritems(), iterkeys(), and itervalues() are called with no intervening modifications to the dictionary, the lists will directly correspond.

开启 2.x documentation3.x documentation .

关于Python 字典 : are keys() and values() always the same order?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/835092/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com