我正在尝试用甲酸消化这个字符串,但我正在尝试计算消化后得到的每个片段,我只想知道如何将字典的值添加到我的新列表集中. (任何建议将不胜感激)
import string
aa_seq = 'MLCPWNFLLKPRYRGKYEPGSSPAADLNNNEKGIGNEKSLVNGHIPNCETINPhSKSFP'
formic_acid = aa_seq.replace('A', 'A|').replace('N', 'N|').upper().split('|')
formate = list(formic_acid)
weights = {'A': 71.04, 'C': 103.01, 'D': 115.03, 'E': 129.04, 'F': 147.07,
'G': 57.02, 'H': 137.06, 'I': 113.08, 'K': 128.09, 'L': 113.08,
'M': 131.04, 'N': 114.04, 'P': 97.05, 'Q': 128.06, 'R': 156.10,
'S': 87.03, 'T': 101.05, 'V': 99.07, 'W': 186.08, 'Y': 163.06 }
weight = []
for acid in formate:
weight = weight + weights[acid]
print "The molecular weight of this protein is", weight
Đầu ra:
Traceback (cuộc gọi gần đây nhất):
File "r.py", line 15, in
weight = weight + weights[acid]
KeyError: 'MLCPWN'
如果你想要权重的总和,得到酸中每个字母的总和:
for acid in formate:
weight = sum(weights[a] for a in acid)
print "The molecular weight of this protein is", weight
如果你想要列表中的重量和酸:
weight_list = []
for acid in formate:
weight = sum(weights[a] for a in acid)
weight_list.append((acid,weight))
print "The molecular weight of this protein is", weight
aa_seq = 'MLCPWNFLLKPRYRGKYEPGSSPAADLNNNEKGIGNEKSLVNGHIPNCETINPhSKSFP'
formic_acid = aa_seq.replace('A', 'A|').replace('N', 'N|').upper().split('|')
weights = {'A': 71.04, 'C': 103.01, 'D': 115.03, 'E': 129.04, 'F': 147.07,
'G': 57.02, 'H': 137.06, 'I': 113.08, 'K': 128.09, 'L': 113.08,
'M': 131.04, 'N': 114.04, 'P': 97.05, 'Q': 128.06, 'R': 156.10,
'S': 87.03, 'T': 101.05, 'V': 99.07, 'W': 186.08, 'Y': 163.06 }
weight_list = []
for acid in formic_acid:
weight = sum(weights[a] for a in acid)
weight_list.append((acid,weight))
print "The molecular weight of the protein {} is {}".format(acid,weight)
print(weight_list)
The molecular weight of the protein MLCPWN is 744.3
The molecular weight of the protein FLLKPRYRGKYEPGSSPA is 2047.06
The molecular weight of the protein A is 71.04
The molecular weight of the protein DLN is 342.15
The molecular weight of the protein N is 114.04
The molecular weight of the protein N is 114.04
The molecular weight of the protein EKGIGN is 598.29
The molecular weight of the protein EKSLVN is 670.35
The molecular weight of the protein GHIPN is 518.25
The molecular weight of the protein CETIN is 560.22
The molecular weight of the protein PHSKSFP is 780.38
[('MLCPWN', 744.3), ('FLLKPRYRGKYEPGSSPA', 2047.0599999999995), ('A', 71.04), ('DLN', 342.15000000000003), ('N', 114.04), ('N', 114.04), ('EKGIGN', 598.29), ('EKSLVN', 670.3499999999999), ('GHIPN', 518.25), ('CETIN', 560.22), ('PHSKSFP', 780.3799999999999)]
要获得最小值和最大值,只需跟踪整个循环:
mx = None
mn = None
for acid in formic_acid:
weight = sum(weights[a] for a in acid)
if mx is None or weight > mx:
mx = weight
if mn is None or weight < mn:
mn = weight
weight_list.append((acid,weight))
print "The molecular weight of the protein {} is {}".format(acid,weight)
print("The minimum and maximum weights are: {}, {}".format(mn,mx))
Tôi là một lập trình viên xuất sắc, rất giỏi!