简体   繁体   English

通过读取另一个文件对字典值求和

[英]summing dictionary values from reading another file

My assignment is to:我的任务是:

Read the protein sequence from FILE A and calculate the molecular weight of
this protein using the dictionary created above.

So far, I have the code below:到目前为止,我有以下代码:

import pprint
my_dict= {'A':'089Da', 'R':'174Da','N':'132Da','D':'133Da','B':'133Da','C':'121Da','Q':'146Da','E':'147Da',
          'Z':'147Da','G':'075Da','H':'155Da','I':'131Da','L':'131Da','K':'146Da','M':'149Da',
          'F':'165Da','P':'115Da','S':'105Da','T':'119Da','W':'204Da','Y':'181Da','V':'117Da'}
new=sorted(my_dict.items(), key=lambda x:x[1])
print("AA", "  ", "MW")
for key,value in new:
    print(key, " ", value)


with open('lysozyme.fasta','r') as content:
    fasta = content.read()
    for my_dict in fasta:

In which the top part of the code is my dictionary created.其中代码的顶部是我创建的字典。 The task is to ie open the rile and read 'MWAAAA' in the file, and then sum up the values associated with those keys using the dictionary I created.任务是打开文件并读取文件中的“MWAAAA”,然后使用我创建的字典总结与这些键关联的值。 I'm not sure how to proceed after the for loop.我不确定在 for 循环之后如何进行。 Do I use an append function?我是否使用 append function? Would appreciate any advice, thanks!不胜感激任何建议,谢谢!

after read your file, you can check char by char:阅读您的文件后,您可以逐个字符地检查字符:

for char in fasta:
    print(char)

output: output:

M
W
A
A
A
A

then use the char as a key for retrieve value of your dict然后使用 char 作为检索 dict 值的键

summ += my_dict[char]

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

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