使用循环命令创建多个字典变量?

发布于 2024-11-16 07:54:10 字数 1262 浏览 3 评论 0原文

这是我第一次使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞花火° 2024-11-23 07:54:10
popdict = {}
incdict = {}
for ix, city in enumerate(WyomingCnty):
  popdict[city] = dict(zip(Years, popdata.col_values(colx=ix + 1,start_rowx=1,end_rowx=None)
  incdict[city] = dict(zip(Years, incomedata.col_values(colx=ix + 1,start_rowx=1,end_rowx=None)
popdict = {}
incdict = {}
for ix, city in enumerate(WyomingCnty):
  popdict[city] = dict(zip(Years, popdata.col_values(colx=ix + 1,start_rowx=1,end_rowx=None)
  incdict[city] = dict(zip(Years, incomedata.col_values(colx=ix + 1,start_rowx=1,end_rowx=None)
窗影残 2024-11-23 07:54:10

我只想使用另一本字典。如:

import xlrd
wb= xlrd.open_workbook('C:\Python27\Forecast_test.xls')

popdata=wb.sheet_by_name(u'Sheet1')  #Import population data
incomedata=wb.sheet_by_name(u'Sheet2') #Import income data

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

county_dict = {}
for col, county in enumerate(WyomingCnty):
    county_dict[county] = {}
    county_popdata = popdata.col_values(colx=col, start_rowx=1, end_rowx=None)
    county_incdata = incomedata.col_values(colx=col, start_rowx=1, endrowx=None)
    county_dict[county]['population'] = county_popdata
    county_dict[county]['income'] = county_incdata
    county_dict[county]['pop_by_year'] = dict(zip(Years, county_popdata)) 
    county_dict[county]['inc_by_year'] = dict(zip(Years, county_incdata)) 

I would just use another dictionary. As in:

import xlrd
wb= xlrd.open_workbook('C:\Python27\Forecast_test.xls')

popdata=wb.sheet_by_name(u'Sheet1')  #Import population data
incomedata=wb.sheet_by_name(u'Sheet2') #Import income data

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

county_dict = {}
for col, county in enumerate(WyomingCnty):
    county_dict[county] = {}
    county_popdata = popdata.col_values(colx=col, start_rowx=1, end_rowx=None)
    county_incdata = incomedata.col_values(colx=col, start_rowx=1, endrowx=None)
    county_dict[county]['population'] = county_popdata
    county_dict[county]['income'] = county_incdata
    county_dict[county]['pop_by_year'] = dict(zip(Years, county_popdata)) 
    county_dict[county]['inc_by_year'] = dict(zip(Years, county_incdata)) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文