添加字符串的所有匹配项

2024-10-04 01:29:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我被一段代码卡住了。在这段代码中,我试图获得字符串(蛋白质)中出现的所有字符(氨基)。我需要在字符串中找到两个字母(['M','L'])。当我使用.count时,“M”得到1,“L”得到10。问题是我找不到正确的方法将两个字母的计数相加得到11

protein = "MSRSLLLRFLLFLLLLPPLP"
aa = ['M', 'L']
    
    for aminos in aa:
        if aminos in protein:
            protein.count(aminos)

Tags: 方法字符串代码inforcount字母蛋白质
3条回答

有很多方法可以做到这一点。比如说,

  1. 你可以记录总数
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)
  1. 您可以编写一个生成器表达式,并使用sum()aa中每个aminocount()的所有值相加
total = sum(protein.count(amino) for amino in aa)
  1. 您可以迭代蛋白质并检查每个字符是否在aa中。但首先,将aa转换为set以降低成员资格检查的成本
s_aa = set(aa)
total = sum(p in s_aa for p in protein) 

这是因为如果ps_aa中,则p in s_aa的计算结果为True,否则False的计算结果为TrueTrue计为一,False计为零,因此当您sum一组True/False值时,您会得到True值的数量

  1. 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)

Vignesh's ^{} technique与此方法相同。计数元素比Hamza's approach好,因为它只在protein字符串上迭代一次,而不是对aa的每个元素迭代一次。这也是我的第三种或第四种方法优于#1和#2的原因

最简单的方法可能是:

sum(protein.count(a) for a in aa)

您还可以获得单独的计数,如下所示:

all_counts = {a:protein.count(a) for a in aa}

结果:{'M': 1, 'L': 10}

如果只需要总数,您可以进一步求和:

sum(all_counts.values())

结果是:11

有很多方法可以做到这一点。以下是其中之一

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)

相关问题 更多 >