python - Python中的dict()实际上是否像集合模块中的OrderedDict()一样排序
问题描述
引用python文档我有:
在 3.7 版更改: 字典顺序保证为插入顺序。这种行为是 CPython 3.6 的实现细节。
但是下面的代码让我觉得它不是有序的:
from collections import OrderedDict
d1 = dict()
d2 = dict()
d1['a'] = 1
d1['b'] = 2
d2['b'] = 2
d2['a'] = 1
print(d1 == d2)
d3 = OrderedDict()
d4 = OrderedDict()
d3['a'] = 1
d3['b'] = 2
d4['b'] = 2
d4['a'] = 1
print(d3 == d4)
上面的代码生成输出(对于 python 3.7.5):
True
False
本质上 dict() 应该显示与集合模块中的 OrderedDict() 相同的行为,但事实并非如此。请详细说明。
解决方案
推荐阅读
- javafx - JavaFX - SceneBuilder TreItem
- react-redux - React-redux 中的连接有什么意义?
- java - 读取 h2o Mojo Zip 的空指针异常
- oop - CLOS:任意函数的方法组合
- azure - Azure AD B2C 对企业 Azure AD 帐户进行身份验证
- react-native - 在 React 原生 Android 中首次启用 GPS 时出现 Navigator.geolocation 错误(Provider gps 暂时不可用)
- python - Ajax 函数不适用于 django 模板中“for”播放的下一个元素
- java - 如何从 BindingAdapter 访问数据绑定变量
- vue.js - 如何对 Vue JS 路由器手表进行单元测试
- ruby-on-rails - 如果记录有效,则在签入时停止加载关联模型