python2 dict 乱序_为什么我的python dict变得无序?
參見英文答案 >
Why is python ordering my dictionary like so?????????????????????????????????????3個
>
Dictionaries: How to keep keys/values in same order as declared?????????????????????????????????????12個
我用這個來制作一個多年來以某個字母開頭的名字百分比圖.當繪制(和打印)我的詞典(letter_d)時,鍵是無序的,而不是像它們被添加到詞典中一樣順序.有沒有辦法解決這個問題,我相信我會按順序將它們添加到dict中.如果沒有,我可以創建一個方法連接我的散點圖的點,以模擬正確的線圖?
import csv
import matplotlib.pyplot as plt
start = 1880
stop = 1890
x = []
years = range(start, stop +1)
print years
letter_d = {}
year_d = {}
alphabet = ['Z']#,'C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
for i in alphabet:
letter_d[i] = 0
for year in years:
filename = 'yob' + str(year) + '.txt'
z = open(filename)
year_d[int(year)] = 0
letter_d[i] = year_d
c = 0
d = 0
for line in z:
y = line.strip().split(',')
y.remove(y[1])
c += int(y[1])
if i in y[0]:
d += int(y[1])
year_d[year] = float(d)/float(c)
#x.append(y)
#print year_d
print year_d.keys()
plt.plot(year_d.keys(), year_d.values())
plt.show()
總結
以上是生活随笔為你收集整理的python2 dict 乱序_为什么我的python dict变得无序?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用什么优化器_在机器学习项目中该如何选
- 下一篇: python 变量引用_Python 知