使用循环命令创建多个字典变量?
这是我第一次使用 python。我正在尝试为每个县(总共 23 个)创建一个字典,以年份作为人口和收入值的关键。强大的代码似乎可以工作,但我确信有一种更简单的方法可以使用循环或类来完成它......有什么建议吗?谢谢!!!!!
import xlrd
wb= xlrd.open_workbook('C:\Python27\Forecast_test.xls')
popdata=wb.sheet_by_name(u'Sheet1')
incomedata=wb.sheet_by_name(u'Sheet2')
WyomingCnty =('Albany', 'Big Horn',
'Campbell', 'Carbon', 'Converse',
'Crook', 'Fremont', 'Goshen',
'Hot Springs','Johnson', 'Laramie',
'Lincoln', 'Natrona','Niobrara',
'Park', 'Platte', 'Sheridan', 'Sublette',
'Sweetwater', 'Teton', 'Uinta', 'Washakie', 'Weston','Wyoming')
Years = ('y0','y1','y2','y3','y4','y5','y6','y7','y8','y9','y10',
'y11','y12', 'y13', 'y14', 'y15', 'y16', 'y17', 'y18','y19',
'y20','y21','y22','y23','y24','y25','y26','y27','y28','y29','y30')
AlbanyPop = popdata.col_values(colx=1,start_rowx=1,end_rowx=None)
AlbanyIncome= incomedata.col_values(colx=1,start_rowx=1,end_rowx=None)
AlbanyDict1=dict(zip(Years,AlbanyPop))
AlbanyDict2=dict(zip(Years,AlbanyIncome))
BigHornPop = popdata.col_values(colx=2,start_rowx=1,end_rowx=None)
BigHornIncome= incomedata.col_values(colx=2,start_rowx=1,end_rowx=None)
BigHornDict1=dict(zip(Years,BigHornPop))
BigHornDict2=dict(zip(Years,BigHornIncome))
This is my first time working with python. I'm trying to create a dictionary for each county (23 in total) with year as the key for population and income values. Strong arming the code seems to work, but I'm sure there is an easier way to do it using loops or classes...any suggestions?? Thanks!!!!!
import xlrd
wb= xlrd.open_workbook('C:\Python27\Forecast_test.xls')
popdata=wb.sheet_by_name(u'Sheet1')
incomedata=wb.sheet_by_name(u'Sheet2')
WyomingCnty =('Albany', 'Big Horn',
'Campbell', 'Carbon', 'Converse',
'Crook', 'Fremont', 'Goshen',
'Hot Springs','Johnson', 'Laramie',
'Lincoln', 'Natrona','Niobrara',
'Park', 'Platte', 'Sheridan', 'Sublette',
'Sweetwater', 'Teton', 'Uinta', 'Washakie', 'Weston','Wyoming')
Years = ('y0','y1','y2','y3','y4','y5','y6','y7','y8','y9','y10',
'y11','y12', 'y13', 'y14', 'y15', 'y16', 'y17', 'y18','y19',
'y20','y21','y22','y23','y24','y25','y26','y27','y28','y29','y30')
AlbanyPop = popdata.col_values(colx=1,start_rowx=1,end_rowx=None)
AlbanyIncome= incomedata.col_values(colx=1,start_rowx=1,end_rowx=None)
AlbanyDict1=dict(zip(Years,AlbanyPop))
AlbanyDict2=dict(zip(Years,AlbanyIncome))
BigHornPop = popdata.col_values(colx=2,start_rowx=1,end_rowx=None)
BigHornIncome= incomedata.col_values(colx=2,start_rowx=1,end_rowx=None)
BigHornDict1=dict(zip(Years,BigHornPop))
BigHornDict2=dict(zip(Years,BigHornIncome))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我只想使用另一本字典。如:
I would just use another dictionary. As in: