确定子字符串在Python中的字符串中出现多少次


68

我试图找出一个字符串在一个字符串中出现了多少次。例如:

nStr = '000123000123'

假设我要查找的字符串是123。显然,它在nStr中出现了两次,但是在将这种逻辑实现到Python中时遇到了麻烦。我现在得到的是:

pattern = '123'
count = a = 0
while pattern in nStr[a:]:
    a = nStr[a:].find(pattern)+1
    count += 1
return count

它应该返回的答案是2。此刻,我陷入了无限循环。

我刚刚意识到计数是一种更好的方法,但是出于好奇,有人能看到一种类似于我已经掌握的方法吗?


谢谢Ashwini ..我忘了数了!
user1294377

Answers:


103

用途str.count

>>> nStr = '000123000123'
>>> nStr.count('123')
2

您的代码的有效版本:

nStr = '000123000123'
pattern = '123'
count =0
flag=True
start=0
while flag:
    a = nStr.find(pattern,start)  # find() returns -1 if the word is not found, 
                                  #start i the starting index from the search starts(default value is 0)
    if a==-1:          #if pattern not found set flag to False
        flag=False
    else:               # if word is found increase count and set starting index to a+1
        count+=1        
        start=a+1
print(count)

27

count()此处显示的问题和其他方法是在子字符串重叠的情况下。

例如:"aaaaaa".count("aaa")返回2

如果您希望它返回4 [ (aaa)aaa, a(aaa)aa, aa(aaa)a, aaa(aaa)],则可以尝试如下操作:

def count_substrings(string, substring):
    string_size = len(string)
    substring_size = len(substring)
    count = 0
    for i in xrange(0,string_size-substring_size+1):
        if string[i:i+substring_size] == substring:
            count+=1
    return count

count_substrings("aaaaaa", "aaa")
# 4

不确定是否有更有效的方法,但是我希望这能阐明count()工作原理。


7
需要注意的是的xrange()在Python 3更名为范围()
TawabG

6
import re

pattern = '123'

n =re.findall(pattern, string)

我们可以说,子字符串“模式”在“字符串”中出现len(n)次。


这将计算计数而没有重叠!
B卡斯特

2

如果您正在寻找如何解决重叠案例的问题。

s = 'azcbobobegghaklbob'
str = 'bob'
results = 0
sub_len = len(str) 
for i in range(len(s)):
    if s[i:i+sub_len] == str: 
        results += 1
print (results)

由于以下原因将产生3,因为:[azc(bob)obegghaklbob] [azcbo(bob)egghaklbob]


1

我很新,但是我认为这是一个很好的解决方案?也许?

def count_substring(str, sub_str):
    count = 0
    for i, c in enumerate(str):
        if sub_str == str[i:i+2]:
            count += 1
    return count

0

string.count(substring)在重叠的情况下没有用。

我的方法:

def count_substring(string, sub_string):

    length = len(string)
    counter = 0
    for i in range(length):
        for j in range(length):
            if string[i:j+1] == sub_string:
                counter +=1
    return counter

0

您不必a每次循环都更改。您应该输入:

a += nStr[a:].find(pattern)+1

...代替:

a = nStr[a:].find(pattern)+1

0
def count_substring(string, substring):
         c=0
         l=len(sub_string)
         for i in range(len(string)):
                 if string [i:i+l]==sub_string:
                          c=c+1
         return c
string=input().strip()
sub_string=input().strip()

count= count_substring(string,sub_string)
print(count)

0

如@JoãoPesce和@gaurav所述,count()在子字符串重叠的情况下没有用,请尝试一下...

def count_substring(string, sub_string):
    c=0
    for i in range(len(string)):
        if(string[i:i+len(sub_string)]==sub_string):
            c = c+1
    return c

0
def countOccurance(str,pat):
    count=0
    wordList=str.split()
    for word in wordList:
        if pat in word:
            count+=1
    return count

0

通常我正在使用枚举来解决此类问题:

def count_substring(string, sub_string):
        count = 0
        for i, j in enumerate(string):
            if sub_string in string[i:i+3]:
                count = count + 1
        return count

@ruddy_simonpour您可能已经添加了nStr ='000123000123'和pattern ='123'的count_substring(nStr,pattern)产生2,这是正确的。
–'NaN

-1

def计数(sub_string,string):

count = 0
ind = string.find(sub_string)

while True:
    if ind > -1:
        count += 1
        ind = string.find(sub_string,ind + 1)
    else:
        break
return count
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.