編碼
from __future__ import division
def get_aa_percentage(protein, aa_list=['A','I','L','M','F','W','Y','V']):
protein = protein.upper()
protein_length = len(protein)
total = 0
for aa in aa_list:
aa = aa.upper()
aa_count = protein.count(aa)
total += aa_count
percentage = total * 100 / protein_length
return percentage
assert get_aa_percentage("MSRSLLLRFLLFLLLLPPLP", ["M"]) == 5
assert get_aa_percentage("MSRSLLLRFLLFLLLLPPLP", ['M', 'L']) == 55
assert get_aa_percentage("MSRSLLLRFLLFLLLLPPLP", ['F', 'S', 'L']) == 70
assert get_aa_percentage("MSRSLLLRFLLFLLLLPPLP") == 65
解析
Python assert(斷言)用於判斷一個表達式,在表達式條件為 False 的時候觸發異常。
斷言可以在條件不滿足程序運行的情況下直接返回錯誤,而不必等待程序運行后出現崩潰的情況。
assert expression
#等價於
if not expression:
raise AssertionError