python - 如果不修改,dicts 会保留迭代顺序吗?

标签 python algorithm dictionary hash

如果我在 Python 中有一个字典,并且我遍历它一次,然后再一次,如果我没有插入、删除或更新字典中的任何项目,是否可以保证保留迭代顺序? (但我可能已经查过了)。

最佳答案

这里是 dict.items() documentation说:

dict.items() return a copy of the dictionary’s list of (key, value) pairs.

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

我认为如果您所做的只是迭代,那么假设项目排序不会改变是合理的。

关于python - 如果不修改,dicts 会保留迭代顺序吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1849324/

相关文章:

python - 断言错误 : array([], dtype=int64) != 数组([], dtype=int64)

python - 为什么多个进程在python中具有相同的对象ID

python - 如何使用opencv在视频中绘制虚线

c++ - 使用boost库找到图形的最小切割

swift - Swift 中的奇怪字典

python - 如何在不需要重启 Airflow 网络服务器的情况下更新 Airflow 中的python函数

对字谜词进行分组的算法

c++ - 具有堆和映射等效功能的数据结构

javascript - 删除一个对象的键也会删除另一个对象中的键 - Javascript

firebase - 从 firebase 数据库 flutter 读取项目列表