Why does my python dict become unordered? [duplicate]

血红的双手。 提交于 2019-12-01 09:03:29
Martijn Pieters

Python dictionaries are always unordered.

In your case, you don't need a dictionary at all. Use two lists; one is the years list you already produced from a range, the other for the calculated values:

year_values = []

for year in years:
    # ...
    year_values.append(float(d)/float(c))

plt.plot(years, year_values)

As it was said dictionaries are unordered. To keep your code, there is a way to display your plot sorted. Sorting dictionary keys.

Look

if __name__ == "__main__":
    year_d = {"5": 'b', "3": 'c', "4": 'z', "1":'a'}
    print year_d.keys()
    keys = map(int, year_d.keys())
    keys = map(keys.sort(), keys)
    print keys

original: ['1', '3', '5', '4'] sorted: [1, 3, 4, 5]

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!