Why Does My Python Dict Become Unordered?
I am using this to produce a plot of % of names starting with a certain letter over a span of years. When plotting (and printing) my diction (letter_d) the keys are disordered rath
Solution 1:
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)
Solution 2:
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)
printkeys
original: ['1', '3', '5', '4'] sorted: [1, 3, 4, 5]
Post a Comment for "Why Does My Python Dict Become Unordered?"