我有一个名为aa_seq几百个氨基酸序列的列表中,它看起来像这样:[“AFYIVHPMFSELINFQNEGHECQCQCG”,“KVHSLPGMSDNGSPAVLPKTEFNKYKI”,“RAQVEDLMSLSPHVENASIPKGSTPIP”,“TSTNNYPMVQEQAILSCIEQTMVADAK” ,. ..]。 每个序列长度为27个字母。我必须确定每个位置(1-27)最常用的氨基酸和频率。增加计数器作为一个字典值的循环
到目前为止,我有:
count_dict = {}
counter = count_dict.values()
aa_list = ['A', 'C', 'D', 'E' ,'F' ,'G' ,'H' ,'I' ,'K' ,'L' , #one-letter code for amino acids
'M' ,'N' ,'P' ,'Q' ,'R' ,'S' ,'T' ,'V' ,'W' ,'Y']
for p in range(0,26): #first round:looks at the first position in each sequence
for s in range(0,len(aa_seq)): #goes through all sequences of the list
for item in aa_list: #and checks for the occurrence of each amino acid letter (=item)
if item in aa_seq[s][p]:
count_dict[item] #if that letter occurs at the respective position, make it a key in the dictionary
counter += 1 #and increase its counter (the value, as definded above) by one
print count_dict
它说KeyError异常: 'A',它的指向线count_dict [项目]。所以aa_list的项目显然不能用这种方式添加为关键字..?我怎么做?它也给出了一个错误,“'int'对象不可迭代”关于计数器。如何可以增加柜台?
什么是你想用'count_dict [项目]'?即使该词典中存在“item”,只要查找该值并立即将其丢弃;你不会在那里分配任何东西。 –
另外,'counter'被定义为count_dict开始时的值列表;它是一个空列表,因为count_dict是空的。所以'counter + = 1'没有意义,因为你不能在列表中添加一个整数。 –
与像C++这样的语言不同,您可以简单地引用它们来初始化字典(映射)条目,但在python中,您需要显式初始化字典条目。 – Unlocked