如何计算Python中字符串中给定子字符串的出现次数?
例如:
>>> 'foo bar foo'.numberOfOccurrences('foo')
2
Run Code Online (Sandbox Code Playgroud)
jsb*_*eno 302
string.count(substring), 像:
>>> "abcdabcva".count("ab")
2
Run Code Online (Sandbox Code Playgroud)
更新:如评论中所指出的,这是为非重叠事件执行此操作的方法.如果你需要计算重叠的事件,你最好检查一下答案:" Python正则表达式找到所有重叠的匹配? ",或者只是检查我的其他答案.
Aru*_*tri 21
s = 'arunununghhjj'
sb = 'nun'
results = 0
sub_len = len(sb)
for i in range(len(s)):
if s[i:i+sub_len] == sb:
results += 1
print results
Run Code Online (Sandbox Code Playgroud)
Don*_*ion 19
根据您的真实含义,我提出以下解决方案:
1)您指的是空格分隔的子字符串列表,并想知道所有子字符串中的子字符串位置编号是什么:
s = 'sub1 sub2 sub3'
s.split().index('sub2')
>>> 1
Run Code Online (Sandbox Code Playgroud)
2)你的意思是字符串中子字符串的char位置:
s.find('sub2')
>>> 5
Run Code Online (Sandbox Code Playgroud)
3)你的意思是(非重叠)计数一个SU-bstring的外观:
s.count('sub2')
>>> 1
s.count('sub')
>>> 3
Run Code Online (Sandbox Code Playgroud)
Anu*_*pta 10
您可以使用两种方式计算频率:
使用count()in str:
a.count(b)
或者,您可以使用:
len(a.split(b))-1
a字符串在哪里,是b要计算其频率的子字符串.
要在Python 3中查找字符串中子字符串的重叠出现,此算法将执行以下操作:
def count_substring(string,sub_string):
l=len(sub_string)
count=0
for i in range(len(string)-len(sub_string)+1):
if(string[i:i+len(sub_string)] == sub_string ):
count+=1
return count
Run Code Online (Sandbox Code Playgroud)
我自己检查了这个算法并且它有效.
在给定字符串中查找重叠子字符串的最佳方法是使用python正则表达式,它将使用正则表达式库找到所有重叠匹配.以下是如何执行它左边是子字符串,在右边你将提供匹配的字符串
print len(re.findall('(?=aa)','caaaab'))
3
Run Code Online (Sandbox Code Playgroud)
小智 7
场景 1:单词在句子中的出现。例如:str1 = "This is an example and is easy"。“是”这个词的出现。让我们str2 = "is"
count = str1.count(str2)
Run Code Online (Sandbox Code Playgroud)
场景 2:句子中模式的出现。
string = "ABCDCDC"
substring = "CDC"
def count_substring(string,sub_string):
len1 = len(string)
len2 = len(sub_string)
j =0
counter = 0
while(j < len1):
if(string[j] == sub_string[0]):
if(string[j:j+len2] == sub_string):
counter += 1
j += 1
return counter
Run Code Online (Sandbox Code Playgroud)
谢谢!
当前涉及方法的最佳答案count并不真正计算重叠事件,也不关心空子字符串.例如:
>>> a = 'caatatab'
>>> b = 'ata'
>>> print(a.count(b)) #overlapping
1
>>>print(a.count('')) #empty string
9
Run Code Online (Sandbox Code Playgroud)
如果我们考虑重叠的子串,第一个答案应该2不是1.至于第二个答案,如果空子字符串返回0作为asnwer则更好.
以下代码负责这些事情.
def num_of_patterns(astr,pattern):
astr, pattern = astr.strip(), pattern.strip()
if pattern == '': return 0
ind, count, start_flag = 0,0,0
while True:
try:
if start_flag == 0:
ind = astr.index(pattern)
start_flag = 1
else:
ind += 1 + astr[ind+1:].index(pattern)
count += 1
except:
break
return count
Run Code Online (Sandbox Code Playgroud)
现在我们运行它:
>>>num_of_patterns('caatatab', 'ata') #overlapping
2
>>>num_of_patterns('caatatab', '') #empty string
0
>>>num_of_patterns('abcdabcva','ab') #normal
2
Run Code Online (Sandbox Code Playgroud)
这个问题不是很清楚,但我会回答你表面上的问题。
字符串 S,长度为 L 个字符,其中 S[1] 是字符串的第一个字符,S[L] 是最后一个字符,具有以下子字符串:
因此,在长度为 L 的字符串中有 0.5*L*(L+1) + 1 个子字符串。在 Python 中呈现该表达式,您将获得字符串中存在的子字符串数量。
一种方法是使用re.subn. 'hello'例如,要计算任意混合情况下出现的次数,您可以执行以下操作:
import re
_, count = re.subn(r'hello', '', astring, flags=re.I)
print('Found', count, 'occurrences of "hello"')
Run Code Online (Sandbox Code Playgroud)