确定Python中字符串中出现子串的次数

时间:2021-04-25 23:54:19

I am trying to figure out how many times a string occurs in a string. For example:

我试图弄清楚字符串中出现字符串的次数。例如:

nStr = '000123000123'

Say the string I want to find is 123. Obviously it occurs twice in nStr but I am having trouble implementing this logic into Python. What I have got at the moment:

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

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

The answer it should return is 2. I'm stuck in an infinite loop at the moment.

它应该返回的答案是2.我现在陷入无限循环。

I was just made aware that count is a much better way to do it but out of curiosity, does anyone see a way to do it similar to what I have already got?

我刚才意识到计数是一种更好的方法,但出于好奇,有没有人看到类似于我已经得到的方法呢?

5 个解决方案

#1


58  

Use str.count:

使用str.count:

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

A working version of your code:

代码的工作版本:

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)

#2


16  

The problem with count() and these methods shown here is the case of overlapping substrings.

这里显示的count()和这些方法的问题是重叠子串的情况。

For example: "aaaaaa".count("aaa") returns 2

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

If you want it to return 4 [(aaa)aaa, a(aaa)aa, aa(aaa)a, aaa(aaa)] you might try something like this:

如果你想要它返回4 [(aaa)aaa,aa(aaa)aa,aa(aaa)a,aaa(aaa)]你可能会尝试这样的事情:

def my_count(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

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

Don't know if there's a better way of doing it, but posting just to clarify the way count() works.

不知道是否有更好的方法,但发布只是为了澄清count()的工作方式。

#3


5  

import re

pattern = '123'

n =re.findall(pattern, string)

We can say that the substring 'pattern' appears len(n) times in 'string'.

我们可以说子串'pattern'在'string'中出现len(n)次。

#4


0  

string.count(substring) is not useful in case of overlapping.

string.count(substring)在重叠时没用。

My approach:

我的方法:

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

#5


0  

You are not changing a with each loop. You should put:

你没有改变每个循环。你应该把:

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

...instead of:

...代替:

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

#1


58  

Use str.count:

使用str.count:

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

A working version of your code:

代码的工作版本:

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)

#2


16  

The problem with count() and these methods shown here is the case of overlapping substrings.

这里显示的count()和这些方法的问题是重叠子串的情况。

For example: "aaaaaa".count("aaa") returns 2

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

If you want it to return 4 [(aaa)aaa, a(aaa)aa, aa(aaa)a, aaa(aaa)] you might try something like this:

如果你想要它返回4 [(aaa)aaa,aa(aaa)aa,aa(aaa)a,aaa(aaa)]你可能会尝试这样的事情:

def my_count(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

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

Don't know if there's a better way of doing it, but posting just to clarify the way count() works.

不知道是否有更好的方法,但发布只是为了澄清count()的工作方式。

#3


5  

import re

pattern = '123'

n =re.findall(pattern, string)

We can say that the substring 'pattern' appears len(n) times in 'string'.

我们可以说子串'pattern'在'string'中出现len(n)次。

#4


0  

string.count(substring) is not useful in case of overlapping.

string.count(substring)在重叠时没用。

My approach:

我的方法:

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

#5


0  

You are not changing a with each loop. You should put:

你没有改变每个循环。你应该把:

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

...instead of:

...代替:

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