如何计算给定子字符串在Python字符串中出现的次数?
例如:
>>> 'foo bar foo'.numberOfOccurrences('foo')
2
若要获取子字符串的索引,请参见如何查找子字符串的所有出现?。
如何计算给定子字符串在Python字符串中出现的次数?
例如:
>>> 'foo bar foo'.numberOfOccurrences('foo')
2
若要获取子字符串的索引,请参见如何查找子字符串的所有出现?。
当前回答
def count_substring(string, sub_string):
k=len(string)
m=len(sub_string)
i=0
l=0
count=0
while l<k:
if string[l:l+m]==sub_string:
count=count+1
l=l+1
return count
if __name__ == '__main__':
string = input().strip()
sub_string = input().strip()
count = count_substring(string, sub_string)
print(count)
其他回答
一种方法是使用re.subn。例如,要数的数量 'hello'出现在任何混合情况下,你可以做:
import re
_, count = re.subn(r'hello', '', astring, flags=re.I)
print('Found', count, 'occurrences of "hello"')
使用Python 3.8中引入的赋值操作符,我们可以编写一个简短的函数,在循环中使用str.find()来查找字符串中目标子字符串的重叠实例。已经有一些其他的解决方案使用相同的方法,但这个更短,更快。
赋值表达式不仅用于在last-found实例之后的字符处开始下一个查找操作,还为while循环提供了终端表达式。Str.find()如果没有找到子字符串,则返回-1,在此基础上加上1将得到0,这是false,因此在没有找到更多匹配时退出循环。
# count overlapping occurrences of a substring in a string
def count_overlapping(haystack, needle, start=0, count=0):
while start := haystack.find(needle, start) + 1:
count += 1
return count
print(count_overlapping("moomoooo", "oo")) # 4
为了进一步优化性能,我们可以查阅草堆。在循环外找到一次,并将其存储在一个局部变量中。这将是更快时,有超过一对夫妇的比赛。
# count overlapping occurrences of a substring in a string
def count_overlapping(haystack, needle, start=0, count=0):
haystack_find = haystack.find
while start := haystack_find(needle, start) + 1:
count += 1
return count
这个问题不是很清楚,但我可以回答你表面上的问题。
一个长度为L个字符的字符串S,其中S[1]是字符串的第一个字符,S[L]是最后一个字符,它有以下子字符串:
空字符串”。这里有一个。 对于从1到L的每一个值A,对于从A到L的每一个值B,字符串S[A]..S[B] (包容)。有L + L-1 + L-2 +…1个字符串,对于a 共0.5*L*(L+1)。 注意,第二项包括S[1]..S[L], 即整个原始字符串S。
所以,在长度为L的字符串中有0.5*L*(L+1) +1个子字符串,在Python中呈现这个表达式,你就有了字符串中出现的子字符串的数量。
目前涉及方法计数的最佳答案并不能真正计算重叠出现的次数,也不关心空子字符串。 例如:
>>> a = 'caatatab'
>>> b = 'ata'
>>> print(a.count(b)) #overlapping
1
>>>print(a.count('')) #empty string
9
如果我们考虑重叠的子字符串,第一个答案应该是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
现在当我们运行它时:
>>>num_of_patterns('caatatab', 'ata') #overlapping
2
>>>num_of_patterns('caatatab', '') #empty string
0
>>>num_of_patterns('abcdabcva','ab') #normal
2
如果你想数整个字符串,这是可行的。
stri_count="If you're looking to count the whole string this can works"
print(len(stri_count))