total = 0
for aminos in aa:
# No need to check if aminos in protein because .count() returns 0 if that's the case
total += protein.count(aminos)
您可以编写一个生成器表达式,并使用sum()将aa中每个amino的count()的所有值相加李>
total = sum(protein.count(amino) for amino in aa)
您可以迭代蛋白质并检查每个字符是否在aa中。但首先,将aa转换为set以降低成员资格检查的成本李>
s_aa = set(aa)
total = sum(p in s_aa for p in protein)
这是因为如果p在s_aa中,则p in s_aa的计算结果为True,否则False的计算结果为TrueTrue计为一,False计为零,因此当您sum一组True/False值时,您会得到True值的数量
对protein中的所有字符进行计数,然后对您关心的字符进行计数之和:
counts = {}
for p in protein:
ct = counts.get(p, 0) # get counts[p], default to 0 if not exists
counts[p] = ct + 1
total = sum(counts.get(amino, 0) for amino in aa)
from collections import Counter
protein = "MSRSLLLRFLLFLLLLPPLP"
aminos = ['M', 'L']
# Count occurrences of all characters
amino_counter = Counter(protein)
total_count = 0
# Only consider the counts of aminos that matter
for amino in aminos:
total_count += amino_counter.get(amino, 0)
print(total_count)
有很多方法可以做到这一点。比如说,
sum()
将aa
中每个amino
的count()
的所有值相加李>aa
中。但首先,将aa
转换为set
以降低成员资格检查的成本李>这是因为如果
p
在s_aa
中,则p in s_aa
的计算结果为True
,否则False
的计算结果为True
True
计为一,False
计为零,因此当您sum
一组True/False
值时,您会得到True
值的数量protein
中的所有字符进行计数,然后对您关心的字符进行计数之和:Vignesh's ^{} technique 与此方法相同。计数元素比Hamza's approach好,因为它只在
protein
字符串上迭代一次,而不是对aa
的每个元素迭代一次。这也是我的第三种或第四种方法优于#1和#2的原因最简单的方法可能是:
您还可以获得单独的计数,如下所示:
结果:
{'M': 1, 'L': 10}
如果只需要总数,您可以进一步求和:
结果是:
11
有很多方法可以做到这一点。以下是其中之一
相关问题 更多 >
编程相关推荐