我正在试图找出这段代码的原因:
states = {
'Oregon': 'OR',
'Florida': 'FL',
'California': 'CA',
'New York': 'NY',
'Michigan': 'MI'
}
print '-' * 10
for state, abbrev in states.items():
print "%s is abbreviated %s" % (state, abbrev)
给出了这个输出:
----------
California is abbreviated CA
Michigan is abbreviated MI
New York is abbreviated NY
Florida is abbreviated FL
Oregon is abbreviated OR
按此特定顺序出现?输出不应该按字典中的顺序排列吗?
即。我认为它会像:
Oregon is abbreviated OR
Florida is abbreviated FL
California is abbreviated CA
New York is abbreviated NY
Michigan is abbreviated MI
答案 0 :(得分:0)
字典没有固有的顺序。如果您关心字典中的项目顺序,请使用collections.OrderedDict。