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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值