确定在字符串中找到的子字符串中的次数 Python

我正在尝试弄清楚这些线在字符串中找到了多少次。 例如:


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. 目前我被困在无限循环中。

我刚刚发现图表是一种更好的方法,但是从好奇心,有人看到了一种方法,让它看起来像我已经收到过的?
已邀请:

三叔

赞同来自:

使用
http://docs.python.org/library ... 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/

董宝中

赞同来自:

一个问题
count//

此处示出的这些方法是重叠子字符串的情况。

例如:
"aaaaaa".count/"aaa"/

回报 2

如果你想让他回来 4 [
/aaa/aaa, a/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


我不知道是否有更好的方法来做到这一点,但帖子很容易澄清它是如何工作的
count//

.

喜特乐

赞同来自:

import re

pattern = '123'

n =re.findall/pattern, string/


我们可以说那个问题 'pattern' 出现 len /n/ 曾经B. 'string'.

诸葛浮云

赞同来自:

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


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] [azcbobobegghakl/bob/]

君笑尘

赞同来自:

我是漂亮的纽伯,但我认为这是一个很好的解决方案? 也许?


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

快网

赞同来自:

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

涵秋

赞同来自:

你没有改变
a

每个周期。 你必须放:


a += nStr[a:].find/pattern/+1


...

反而:


a = nStr[a:].find/pattern/+1

喜特乐

赞同来自:

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

二哥

赞同来自:

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/

董宝中

赞同来自:

如上所述 @João Pesce 和 @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

詹大官人

赞同来自:

我经常使用 enumerate 对于这样的问题:


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

诸葛浮云

赞同来自:

格拉夫德/Substro,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

要回复问题请先登录注册