我有一個名爲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