gpt4 book ai didi

python - 为什么python字典的元素没有按顺序打印

转载 作者:太空宇宙 更新时间:2023-11-04 06:57:45 32 4
gpt4 key购买 nike

我有这本字典

sr = {'DTW':'CNT','FWA':'CNT','GRR':'CNT','IND':'CNT','MKE':'CNT','MLI':'CNT','MSP':'CNT','OMA':'CNT','ORD':'CNT','SBN':'CNT','STL':'CNT','BDL':'EST','BOS':'EST','BWI':'EST','CLE':'EST','CMH':'EST','CVG':'EST','EWR':'EST','IAD':'EST','JFK':'EST','MDT':'EST','PHL':'EST','PIT':'EST','ROC':'EST','ABQ':'CNT','AUS':'CNT','DEN':'CNT','DFW':'CNT','ELP':'CNT','IAH':'CNT','LRD':'CNT','MCI':'CNT','MFE':'CNT','MSY':'CNT','OKC':'CNT','SAT':'CNT','TUL':'CNT','ATL':'EST','BNA':'EST','CLT':'EST','JAX':'EST','MCO':'EST','MEM':'EST','MIA':'EST','RDU':'EST','RIC':'EST','SDF':'EST','SJU':'EST','TPA':'EST','ANC':'WST','HNL':'WST','LAS':'WST','LAX':'WST','PDX':'WST','PHX':'WST','RNO':'WST','SAN':'WST','SEA':'WST','SFO':'WST','SLC':'WST','SMF':'WST','TUS':'WST'}

for s, r in sr.iteritems():
print s, r

但在打印时它是从

开始打印的
JFK EST
MKE CNT

为什么?为什么打印不盯着 DTW?

最佳答案

因为dict的实现是hashmap或者hash table , 它不会按顺序存储元素。

来自docs

CPython implementation detail: Keys and values are listed in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions.

如果顺序很重要,您应该使用 OrderedDict

关于python - 为什么python字典的元素没有按顺序打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5752802/

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