我在各处搜索时,人们都说Python字典没有任何顺序。 当我运行代码1时,每次输出的结果都不同(随机顺序)。但是当我运行代码2时,它总是显示相同的排序输出。为什么第二个片段中的字典是有序的?
#code 1
d = {'one': 1, 'two': 2, 'three': 3, 'four': 4}
for a, b in d.items():
print(a, b)
#code 2
d = {1: 10, 2: 20, 3: 30, 4: 40}
for a, b in d.items():
print(a, b)
输出
代码1:
four 4
two 2
three 3
one 1
再次展示代码1:
three 3
one 1
two 2
four 4
代码 2(始终):
1 10
2 20
3 30
4 40
-1
的哈希值是-2
,而非常大的整数也需要降低其哈希值,例如hash(10**20)
。 - wim