简体   繁体   English

将文件中的连续两行作为值,密钥对读取到字典中

[英]Read two consecutive lines from a file into a dictionary as value, key pairs

if Class.lower() == ("classa"):
    print("Showing results for ClassA")
NewDict = {}
with open ("ClassA.txt","r") as A:
     for line in A:

my list is below and I would like to make the user's name their key and their score the value. 我的列表在下面,我想将用户的名称作为键,并将其分数作为值。

3
Dominic
1
Eric
7
Joe
4
Sasha
8
Olivia
6
Callum

I am stuck on making the users name, eg Dominic the key for the dictionary and their score as their value. 我一直坚持要为用户命名,例如Dominic作为字典的键,以及他们的得分作为他们的价值。 I cannot do this because in my text file they are on different lines the score being on the first line then every other line and the name being on the second line and every other line from there. 我无法执行此操作,因为在我的文本文件中,它们位于不同的行中,得分位于第一行,然后是每隔一行,名称位于第二行,然后是每隔一行。

The following solution makes a few assumptions. 以下解决方案作了一些假设。 The first is that the number of lines in your file is even because there is always a line with the score followed by a line with the name. 第一个是文件中的行数是偶数,因为总会有一行包含乐谱,然后是带有名称的行。 It also assumes that there are no duplicate names and the format you are showing is honored. 它还假设没有重复的名称,并且您所显示的格式是正确的。

With that said, this solution shows you how to build a dictionary mapping names to scores and should point you in the right direction. 话虽如此,该解决方案将向您展示如何构建将名称映射到乐谱的字典,并应为您指明正确的方向。

>>> with open('ClassA.txt') as f:
...     scores = {name.strip():int(score) for score, name in zip(f, f)}
... 
>>> scores
{'Callum': 6, 'Sasha': 4, 'Olivia': 8, 'Joe': 7, 'Dominic': 3, 'Eric': 1}

Further reading: How do I read two lines from a file at a time using python 进一步阅读: 如何使用python一次从文件读取两行

For people stumbling across this question using Python 2: consider using itertools.izip for memory efficiency, especially if the file is large. 对于使用Python 2绊脚石的人们,考虑使用itertools.izip来提高内存效率,尤其是在文件较大的情况下。

edit : 编辑

Most of your assumptions are correct however names are used again three times each , the code which you have given would work if the name was not repeated. 您的大多数假设都是正确的,但是名称每次都要重复使用3次 ,如果不重复名称,您给出的代码将可以使用。

You should have mentioned that. 您应该已经提到过。 In that case, I suggest that you append the scores to a list. 在这种情况下,我建议您将分数添加到列表中。 You can't have the same key twice in a dictionary. 您在字典中不能两次拥有相同的键。

>>> from collections import defaultdict
>>> scores = defaultdict(list)
>>> 
>>> with open('ClassA.txt') as f:
...     for score, name in zip(f, f):
...         scores[name.strip()].append(int(score))
... 
>>> scores
defaultdict(<type 'list'>, {'Callum': [6, 12, 18], 'Sasha': [4, 10, 16], 'Olivia': [5, 11, 17], 'Joe': [3, 9, 15], 'Dominic': [1, 7, 13], 'Eric': [2, 8, 14]})

The inputfile I used for the demo looks like this: 我用于演示的输入文件如下所示:

1
Dominic
2
Eric
3
Joe
4
Sasha
5
Olivia
6
Callum
7
Dominic
8
Eric
9
Joe
10
Sasha
11
Olivia
12
Callum
13
Dominic
14
Eric
15
Joe
16
Sasha
17
Olivia
18
Callum

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM