Python字典初始化

Python dictionary initialisation

本问题已经有最佳答案,请猛点这里访问。

我用以下键值对初始化了字典:

1
count = {"a":0,"b":0,"c":0,"d":0,"e":0,"f":0,"g":0,"h":0,"i":0,"j":0,"k":0,"l":0,"m":0,"n":0,"o":0,"p":0,"q":0,"r": 0,"s":0,"t": 0,"u":0,"v":0,"w":0,"x":0,"y":0,"z":0}

但是,当我打印时,除了第一对和最后一对以外,其他对已经互换:

1
{'a': 0, 'c': 0, 'b': 0, 'e': 0, 'd': 0, 'g': 0, 'f': 0, 'i': 0, 'h': 0, 'k': 0, 'j': 0, 'm': 0, 'l': 0, 'o': 0, 'n': 0, 'q': 0, 'p': 0, 's': 0, 'r': 0, 'u': 0, 't': 0, 'w': 0, 'v': 0, 'y': 0, 'x': 0, 'z': 0}

这给了我对我试图解决的问题的错误答案。为什么会这样?


如果要保留顺序,应使用Collections.OrderedDict而不是标准字典。