I have a .txt file that I opened and converted to a list using the .readlines method.
我有一个.txt文件,我打开并使用.readlines方法转换为列表。
with open('superheroes.txt') as f:
hero = f.readlines()
I read it into an empty dictionary and since the file had a lot of duplicate strings but unique numbers, I chose to make the unique integers the keys and the duplicate strings the values using this code:
我把它读成一个空的字典,因为文件有很多重复的字符串但是有唯一的数字,我选择使用这个代码将唯一的整数设为键,重复的字符串使用这些代码:
heroes = {}
for i in range(len(hero)):
name, num = hero[i].strip().split("\t")
heroes[int(num)] = name
The output of the print statement gives something similar to this:
print语句的输出提供类似于此的内容:
{1867: 'Superman', 2904: 'Batman', 783: 'Wonderwoman', 4637: 'Superman', 8392: 'Batman', etc}
{1867:'超人',2904:'蝙蝠侠',783:'神奇女侠',4637:'超人',8392:'蝙蝠侠'等}
So far, I was able to append all the keys of a certain value into a unique list and manually sorted them using a for loop and an if statement:
到目前为止,我能够将特定值的所有键附加到唯一列表中,并使用for循环和if语句手动对它们进行排序:
superheroes = list(set(heroes.values())) #Gets all the unique values into one list
print(superheroes.sort()) #Ensures that the positions don't change when sorting
supes = []
bats = []
wonder = []
flash = []
cap = []
for entry in heroes:
if heroes[entry] == superheroes[0]:
bats.append(entry)
elif heroes[entry] == superheroes[1]:
cap.append(entry)
elif heroes[entry] == superheroes[2]:
flash.append(entry)
elif heroes[entry] == superheroes[3]:
supes.append(entry)
else:
wonder.append(entry)
print("People Saved: ")
print("Batman ", sum(bats))
print("Captain America ", sum(cap))
print("Flash ", sum(flash))
print("Superman ", sum(supes))
print("Wonderwoman ", sum(wonder))
After this, I would have to find a method to put these values into a new dictionary. My task is to sum the keys and only have the values shown once. Meaning that all the people they saved must be a single integer value that can then be summed together with another dictionary like this.
在此之后,我将不得不找到一种方法将这些值放入一个新的字典中。我的任务是对键求和,只显示一次值。这意味着他们保存的所有人必须是一个整数值,然后可以与另一个这样的字典一起求和。
How can I sum all the keys of each unique value together and display them within the dictionary? I can only use vanilla Python 3.x code and no other modules can be imported.
如何将每个唯一值的所有键相加并在字典中显示?我只能使用vanilla Python 3.x代码,不能导入其他模块。
1 个解决方案
#1
0
I am not sure to get the full picture of your question. You are looking for a way to sum all the key and display them as a dictionary where the key would be the value of your input and the value would be the sum of the key of your input.
我不确定能够全面了解你的问题。您正在寻找一种方法来对所有键求和并将它们显示为字典,其中键是输入的值,值将是输入键的总和。
Would this be helpful
这会有所帮助吗?
input = {1867: 'Superman', 2904: 'Batman', 783: 'Wonderwoman', 4637: 'Superman', 8392: 'Batman'}
output = {v:k for k, v in input.items()}
This produce
>>> {'Superman': 4637, 'Batman': 8392, 'Wonderwoman': 783}
#1
0
I am not sure to get the full picture of your question. You are looking for a way to sum all the key and display them as a dictionary where the key would be the value of your input and the value would be the sum of the key of your input.
我不确定能够全面了解你的问题。您正在寻找一种方法来对所有键求和并将它们显示为字典,其中键是输入的值,值将是输入键的总和。
Would this be helpful
这会有所帮助吗?
input = {1867: 'Superman', 2904: 'Batman', 783: 'Wonderwoman', 4637: 'Superman', 8392: 'Batman'}
output = {v:k for k, v in input.items()}
This produce
>>> {'Superman': 4637, 'Batman': 8392, 'Wonderwoman': 783}