【问题标题】:How to iterate through different dictionaries each time you loop inside a for loop每次在 for 循环中循环时如何遍历不同的字典
【发布时间】: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


【解决方案1】:

以下是我的建议:

def getConstituencies(aFile):

    all_data = {}
    constituency_keys = []
    constituency_idx = []

    # Store all of the information in the text file
    with open(aFile, 'rb') as f:
        lines = f.readlines()

    # Go through the stored info and find all of the constituencies and corresponding indexes
    for idx, line in enumerate(lines):
        if line.split(':')[0] = '_Constituency':
            constituency_keys.append(line.split(':')[1])
            constituency_idx.append(idx)

    # Now go through the stored lines and from the specified index until a blank line, store all the related info into a dictionary. 
    # Then, nest that dictionary into the all_data dictionary
    for idx, c in zip(constituency_idx, constituency_keys):
        temp_dict = {}
        for line in lines[idx+1:]:
            if line == "":
                break
            temp_dict[line.split(':')[0]] = line.split(':')[1]
        all_data[c] = temp_dict 

然后,访问信息就是查询选区的问题:

all_data['East Midlands']['Green']

我还建议查看 Pandas 以开始将您的数据分类到 DataFrames - 这将使您的数据处理工作更轻松

欢迎其他用户提出建议和反馈。

【讨论】:

  • 接受您的欢迎来自其他用户的建议和反馈。使用readlines() 然后遍历行列表没有多大意义,当您可以直接做for line in file_obj:。此外,变量和函数名称应遵循lower_case_with_underscores 样式。
  • 这很公平。更简洁的解决方案是执行一个循环,然后进行一系列检查以查看该行是否为空或包含字符串 '_Constituency'
  • 您认为在这里使用正则表达式是否值得?
  • 我认为正则表达式在这种情况下可能有点矫枉过正——取决于我们想要制作的数据结构,我们唯一需要注意的是下划线和冒号。你怎么看?
  • 如果没有更多 OP 的数据,很难判断。我确实认为我们可以设计一些将所有party:votes 对作为组返回的东西,这非常实用。我希望 OP 分享他们的数据,我很想试一试。
猜你喜欢
  • 1970-01-01
  • 2023-03-18
  • 2013-02-20
  • 2011-03-18
  • 2020-11-10
  • 2016-01-11
相关资源
最近更新 更多