为什么dict和dict.items()的顺序不同?
发布于 2021-01-29 17:08:51
>>> d = {'A':1, 'b':2, 'c':3, 'D':4}
>>> d
{'A': 1, 'D': 4, 'b': 2, 'c': 3}
>>> d.items()
[('A', 1), ('c', 3), ('b', 2), ('D', 4)]
当我调用d.items()时,顺序是否会随机化两次?还是只是以不同的方式随机化?有没有其他方法可以使d.items()返回与d相同的顺序?
编辑:似乎是IPython事物,它会自动对字典进行排序。通常,dict和dict.items()的顺序应相同。
关注者
0
被浏览
160
1 个回答