通过从其他词典中提取数据来编译字典(Compiling a dictionary by pulling data from other dictionaries)

系统教程 行业动态 更新时间:2024-06-14 16:57:39
通过从其他词典中提取数据来编译字典(Compiling a dictionary by pulling data from other dictionaries)

我正在做一个项目,在其中我从三个不同的数据集中提取数据并将其组合起来查看活动贡献。 为此,我将两个集合中的相关数据转换为字典(canDict和otherDict),其中ID号作为键,我需要的信息(派对从属关系)作为值。 然后我写了一个程序,根据密钥(我的第三组包括这些ID号码)提取党的信息,并与捐赠方的雇主和捐赠的金额相匹配。 这是一个冗长的解释,但我认为这有助于理解这一大块代码。

我的问题是,由于某种原因,我的第三个字典(employerDict)将无法编译。 到这一步结束时,我应该有一个包含雇主作为键的字典,以及一个元组列表作为值,但在运行它之后,字典仍然是空白的。 我已经在这条线上经过了十几次而且我把头发拉了出来 - 我不能为我的生活思考为什么它不起作用,这使得难以寻找答案。 我几乎评论了每一行,试图让它更容易理解。 谁能发现我的错误?

更新:我在最外面的for循环中添加了一个计数器n,以查看程序是否正在迭代。

更新2:我在创建变量party添加了另一个if语句,以防数据[0]中的ID在canDict或otherDict中不存在。 我还从评论中添加了一些已经建议的修复程序。

n=0 with open(path3) as f: # path3 is a txt file for line in f: n+=1 if n % 10000 == 0: print(n) data = line.split("|") # Splitting each line into its entries (delimited by the symbol |) party = canDict.get(data[0]) # data[0] is an ID number. canDict and otherDict contain these IDs as keys with party affiliations as values if party is None: party = otherDict[data[0]] # If there is no matching ID number in canDict, search otherDict if party is None: party = 'Other' else: print('ERROR: party is None') x = (party, int(data[14])) # Creating a tuple of the the party (found through the loop) and an integer amount from the file path3 employer = data[11] # Index 11 in path3 is the employer of the person if employer != '': value = employerDict.get(employer) # If the employer field is not blank, see if this employer is already a key in employerDict if value is None: employerDict[employer] = [x] # If the key does not exist, create it and add a list including the tuple x as its value else: employerDict[employer].append(x) # If it does exist, add the tuple x to the existing value else: print('ERROR: employer == ''')

I am doing a project in which I extract data from three different data sets and combine it to look at campaign contributions. To do this I turned the relevant data from two of the sets into dictionaries (canDict and otherDict) with ID numbers as keys and the information I need (party affiliation) as values. Then I wrote a program to pull party information based on the key (my third set included these ID numbers as well) and match them with the employer of the donating party, and the amount donated. That was a long winded explanation, but I thought it would help with understanding this chunk of code.

My problem is that, for some reason, my third dictionary (employerDict) won't compile. By the end of this step I should have a dictionary containing employers as keys, and a list of tuples as values, but after running it, the dictionary remains blank. I've been over this line by line a dozen times and I'm pulling my hair out - I can't for the life of me think why it won't work, which is making it hard to search for answers. I've commented almost every line to try to make it easier to understand out of context. Can anyone spot my mistake?

Update: I added a counter, n, to the outermost for loop to see if the program was iterating at all.

Update 2: I added another if statement in the creation of the variable party, in case the ID at data[0] did not exist in canDict or in otherDict. I also added some already suggested fixes from the comments.

n=0 with open(path3) as f: # path3 is a txt file for line in f: n+=1 if n % 10000 == 0: print(n) data = line.split("|") # Splitting each line into its entries (delimited by the symbol |) party = canDict.get(data[0]) # data[0] is an ID number. canDict and otherDict contain these IDs as keys with party affiliations as values if party is None: party = otherDict[data[0]] # If there is no matching ID number in canDict, search otherDict if party is None: party = 'Other' else: print('ERROR: party is None') x = (party, int(data[14])) # Creating a tuple of the the party (found through the loop) and an integer amount from the file path3 employer = data[11] # Index 11 in path3 is the employer of the person if employer != '': value = employerDict.get(employer) # If the employer field is not blank, see if this employer is already a key in employerDict if value is None: employerDict[employer] = [x] # If the key does not exist, create it and add a list including the tuple x as its value else: employerDict[employer].append(x) # If it does exist, add the tuple x to the existing value else: print('ERROR: employer == ''')

最满意答案

感谢大家的所有输入 - 但是,它看起来像我的数据文件的问题,而不是程序的问题。 该死的。

Thanks for all the input everyone - however, it looks like its a problem with my data file, not a problem with the program. Dangit.

更多推荐

本文发布于:2023-04-13 11:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/9a7ce4383ea7721d665a8195cbe69410.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字典   词典   数据   Compiling   dictionaries

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!