Python 计算字符串中的一组字符

Python 计算字符串中的一组字符,python,python-3.x,Python,Python 3.x,我试图计算字符串中的一组字符,“qed”。我的想法是迭代给定字符串中的每个字符,如果N(i),N(i-1),N(i-2)与'qed'匹配,则更新计数,但迄今为止失败。有什么建议吗?谢谢 def test(N): s = ('qed') count = 0 for i in range(len(N)): if N[i] + N[i-1] + N[i-2] == s: count = count + 1

我试图计算字符串中的一组字符,“qed”。我的想法是迭代给定字符串中的每个字符,如果N(i),N(i-1),N(i-2)与'qed'匹配,则更新计数,但迄今为止失败。有什么建议吗?谢谢

def test(N):
    s = ('qed')
    count = 0
        for i in range(len(N)):
            if N[i] + N[i-1] + N[i-2] == s:
                count = count + 1
        return print(count)
test('qedmlqedlolqed')
编辑:为什么投反对票?问题是“有什么建议吗?”我认为这是一个好问题。

修复代码:

def test(N):
    s = 'qed'
    count = 0
    for i in range(len(N)-2):
        if N[i:i+3] == s:
            count += 1
    return count

>>> test('qedmlqedlolqed')
3
def test(N):
  s = 'qed'
  count = 0
  for i in range(2, len(N)):
    if N[i-2] + N[i-1] + N[i] == s:
      count = count + 1
  return count
print(test('qedmlqedlolqed'))
或者更一般地说:

def test(N, s):
    count = 0
    if s:
        for i in range(len(N)-len(s)+1):
            if N[i:i+len(s)] == s:
                count += 1
    return count

>>> test('qedmlqedlolqed', 'qed')
3
>>> test('qedmlqedlolqed', 'ed')
3
>>> test('qedmlqedlolqed', 'd')
3
>>> test('qedmlqedlolqed', '')
0
>>> test('qedmlqedlolqed', 'lol')
1
>>> test('qedmlqedlolqed', 'rofl')
0

或者更简单地使用
str.count()


虽然Stefan的答案简单明了,但这里有另一种方法可以通过列表理解来实现

s='QEDMLQEDOLQED'

结果=len([1表示范围内的i(len)),如果s[i:i+3]=='qed'])
(感谢Stefan)

反复使用
查找

>>> text = 'qedmlqedlolqed'
>>> count = index = 0
>>> while True:
        index = text.find('qed', index) + 1
        if not index:
            break
        count += 1

>>> count
3
我使用了
+1
而不是
+3
来支持重叠出现(如果您使用的是没有重复的字符串,则
'qed'
或任何其他“字符集”都不会出现重叠出现)。

修复代码:

def test(N):
    s = 'qed'
    count = 0
    for i in range(len(N)-2):
        if N[i:i+3] == s:
            count += 1
    return count

>>> test('qedmlqedlolqed')
3
def test(N):
  s = 'qed'
  count = 0
  for i in range(2, len(N)):
    if N[i-2] + N[i-1] + N[i] == s:
      count = count + 1
  return count
print(test('qedmlqedlolqed'))
或者,您可以计算字符串中有多少后缀以qed开头:

def test2(word, sub):
  return sum(1 for i,_ in enumerate(word) if word[i:].startswith(sub))
print(test2('qedmlqedlolqed', 'qed'))
或者您可以直接计算所有子字符串,然后检查您的子字符串的数量:

import collections
def all_substrings(s):
  for i in range(len(s)):
    for j in range(i, len(s)):
      yield s[i:j+1]

def test3(word, sub):
  return collections.Counter(all_substrings(word))[sub]
print(test3('qedmlqedlolqed', 'qed'))

对于in N中的i:
将循环遍历字符串中的字符。。。您希望在长度范围内循环。好的,首先,您必须正确缩进函数体。2:你不必把字符串放在括号里3:你的意思是
对于范围内的i(len(N))
,否则它将在字符上迭代而不是索引4:索引是通过
N[i]
而不是通过
N(i)
5:
i,i-1,i-3
哪里是
i-2
?6:当
i时,字符串将索引不足,您可以编写
count+=1
,但这不是错误(字符串文字周围的数字2:括号也不是错误)@hege_hegedus谢谢。我已经编辑了代码。@hege_hegedus N[I]不会调用N中的字符。相反,它会计算字符的索引。我不想在这里使用计数方法。谢谢你!不尝试使用它,还是尝试不使用它?前者说起来没有意义,后者应该在问题中提到。你想解释一下为什么你使用
range(len(N)-2)
N[i:i+3]
。对于
N[i:i+3]
,我假设你说的是从索引0到索引0+3,然后比较==s,对吗?@misheekoh:对。从字符串开始到字符串结束前3个字符的运行。将它们与目标字符串进行比较
N[i:i+3]
是一个片段,返回从索引
i
开始的3个字符串。对于丢弃列表中的元素,不需要使用
s[i:i+3]
,您可以使用
1
。或者只需使用更直接的
和(s[i:i+3]=“qed”表示范围内的i(len))