【发布时间】:2020-04-06 08:50:39
【问题描述】:
我的问题是每次我的程序遍历 for 循环时,是否可以更改它正在处理的字典(在这种情况下添加信息)。即第一次遍历 dict_1,然后是 dict_2 等。
def getDicts(aFile):
voteFile = open(aFile)
listDicts = [{},{},{},{},{},{},{},{},{},{},{}]
i = 0
for line in voteFile:
lineSplit = line.split(':')
if len(lineSplit) > 1:
key = lineSplit[0].strip()
value = lineSplit[1].strip()
listDicts[i][key] = value
else:
i += 1
return listDicts
程序正在处理的文件包含文本块,其中每行有两个用冒号分隔的术语。每个块之后都有一个新行。这就是为什么有一个while循环检查每行上的术语长度不是2的原因。当程序退出while循环时,我希望它将完成的字典(dict_1)添加到字典列表中,然后重新开始在 .txt 文件中的下一个文本块上,这次将信息添加到 dict_2。
按要求填写 .txt 数据:
_Constituency:East Midlands
_Seats:5
Brexit Party:452321
Liberal Democrats:203989
Labour:164682
Conservative:126138
Green:124630
UKIP:58198
Change UK:41117
Independent Network:7641
Simon Rood (Independent):4511
_Constituency:East of England
_Seats:7
Brexit Party:604715
Liberal Democrats:361563
Green:202460
Conservative:163830
Labour:139490
Change UK:58274
UKIP:54676
English Democrat:10217
Attila Csordas (Independent):3230
_Constituency:London
_Seats:8
Liberal Democrats:608725
Labour:536810
Brexit Party:400257
Green:278957
Conservative:177964
Change UK:117635
UKIP:46497
Animal Welfare:25232
Women's Equality:23766
UK EU:18806
Claudia Mcdowell (Independent):1036
Daze Aghaji (Independent):1018
Roger Hallam (Independent):924
Kofi Klu (Independent):869
Andrea Venzon (Independent):731
Mike Shad (Independent):707
Zoe Lafferty (Independent):436
Andrew Medhurst (Independent):430
Alan Kirkby (Independent):401
Ian Sowden (Independent):254
Henry Muss (Independent):226
_Constituency:North East England
_Seats:3
Brexit Party:240056
Labour:119931
Liberal Democrats:104330
Green:49905
Conservative:42395
UKIP:38269
Change UK:24968
_Constituency:North West England
_Seats:8
Brexit Party:541843
Labour:380193
Liberal Democrats:297507
Green:216581
Conservative:131002
UKIP:62464
Change UK:47237
Tommy Robinson (Independent):38908
English Democrat:10045
UK EU:7125
Mohammad Aslam (Independent):2002
_Constituency:South East England
_Seats:10
Brexit Party:915686
Liberal Democrats:653743
Green:343249
Conservative:260277
Labour:184678
Change UK:105832
UKIP:56487
UK EU:7645
Jason Guy Spencer McMahon (Independent):3650
Socialist (GB):3505
David Victor Round (Independent):2606
Michael Jeffrey Turberville (Independent):1587
_Constituency:South West England
_Seats:6
Brexit Party:611742
Liberal Democrats:385095
Green:302364
Conservative:144674
Labour:108100
UKIP:53739
Change UK:46612
English Democrat:8393
Larch Maxey (Independent):1772
Mothiur Rahman (Independent):755
Neville Seed (Independent):3383
_Constituency:West Midlands
_Seats:7
Brexit Party:507152
Labour:228298
Liberal Democrats:219982
Green:143520
Conservative:135279
UKIP:66934
Change UK:45673
_Constituency:Yorkshire and the Humber
_Seats:6
Brexit Party:470351
Labour:210516
Liberal Democrats:200180
Green:166980
Conservative:92863
UKIP:56100
Yorkshire Party:50842
Change UK:30162
English Democrat:11283
_Constituency:Scotland
_Seats:6
SNP:594553
Brexit Party:233006
Liberal Democrats:218285
Conservative:182476
Labour:146724
Scottish Green:129603
Change UK:30004
UKIP:28418
Gordon Edgar (Independent):6128
Ken Parke (Independent):2049
_Constituency:Wales
_Seats:4
Brexit Party:271404
Plaid Cymru:163928
Labour:127833
Liberal Democrats:113885
Conservative:54587
Green:52660
UKIP:27566
Change UK:24332
【问题讨论】:
-
你好。我认为这更多的是数据结构设计的问题,而不是是否可以将不同的字典发送到您的
for循环(或函数)中的问题。退后一步,想想你想要什么样的数据结构。我建议 1 个字典,键是选区,值是另一个嵌套字典,键是派对,值是投票。 -
您可以将字典放入列表中。你可以有
dicts = [{}, {}, ...]然后dicts[i][key] = value -
@Blorgbeard 哦,我的那可能有用,为什么我没想到,谢谢
-
请将代码/数据作为帖子本身的文本分享,而不是图片。
-
您能解释一下您的程序的用途吗?此外,变量和函数名称应遵循
lower_case_with_underscores样式。
标签: python list dictionary iteration