繁体   English   中英

如何仅使用find和replace查找并计算字符串中所有子字符串的出现次数?

[英]How do I find and count all the occurrences of a substring in a string using only find and replace?

该条目需要更低,最后程序必须打印出现次数。 例如mem。

smthing = str(input())
if (smthing == smthing.lower()):
    smthing.find(mem)

我完全沉溺于此,所以我无法走远。

我忘了告诉我不能使用计数或列表。

怎么样的

string = "hello world hello".lower()
replace = "hello".lower()
count = 0

while string.find(replace) != -1:
    string = string.replace(replace, "", 1)
    count += 1

print count
# => Output
# => 2

要处理重叠的字符串,而不是替换整个子字符串,我们可以只替换单个字符,最好是第一个,从原始字符串replace[0]

string = "boboobobobobob".lower()
replace = "bob".lower()
count = 0 

while string.find(replace) != -1:
    string = string.replace(replace[0], "", 1)
    count += 1

print count
# Output
# => 6

如果你有重叠的字符串,你需要一次更换一个字符:

sub = "bob"
smthing = input()
count = 0

for i in iter(lambda: smthing.find(sub), -1):
    smthing = smthing.replace(sub[0], "", 1)
    count += 1
print(count)

所以对于boboobobobobob你会得到6而不是3。

如果您不能使用计数但可以使用其中一个或另一个,则可以单独使用替换,但这不包括重叠:

print((len(smthing) - len(smthing.replace(sub,""))) / len(sub))

试试这段代码:

smthing = "blablabla"
mem = "bl"
count = 0
if (smthing == smthing.lower()):
    bkpsmthing = smthing # maybe you'll need it later
    while (smthing.find(mem) > -1):
        smthing = smthing.replace(mem, '', 1)
        count += 1
print count

它使用str.find在没有找到任何内容时返回-1的事实,或者指向要停止的最低条目的索引:

返回s中找到子字符串sub的最低索引,使得sub完全包含在s [start:end]中。 失败时返回-1。

它还使用了str.replace可以通过使用maxreplace参数( maxreplace )删除单个条目(最低)的maxreplace 这样我们就会不断删除我们计算过的条目:

...如果给出了可选参数maxreplace,则替换第一个maxreplace事件。

该过程可以这样描述:

find "bl" in "blablabla" -> found at index 0
replace "bl" in "blablabla" -> gives "ablabla"
find "bl" in "ablabla" -> found at index 1
replace "bl" in "ablabla" -> gives "aabla"
find "bl" in "aabla" -> found at index 2
replace "bl" in "aabla" -> gives "aaa"
find "bl" in "aaa" -> not found, returns -1
stop

要对count变量执行相同的操作,请使用这个简单的递归(确保在使用my_count之前验证字符串是小写的):

def my_count(my_string, my_substring):
    if my_string.find(my_substring) > -1:
        new_string = my_string.replace(my_substring, '', 1)
        return my_count(new_string, my_substring) + 1
    return 0

输出:

>>> my_count("blablabla", "bl")
3

递归展开如下:

my_count("blablabla", "bl") =
= my_count("ablabla", "bl") + 1 =
= my_count("aabla", "bl") + 1 + 1 =
= my_count("aaa", "bl") + 1 + 1 + 1 =
= 0 + 1 + 1 + 1 = 3

你没有; 你需要额外的工具,可能只需要基本算法。 例如,如果使用不同长度的子字符串替换子字符串,则可以将结果的长度与原始字符串进行比较,以计算出现的次数。 另一种选择是使用start参数来查找其他出现的情况。 我很想知道你尝试了什么; 您显示的代码不会产生任何结果。

对于记录,您可以使用string.replace()执行此操作:

smthing = str(input())  
word='mem'
x=0

while word in smthing:
    smthing=smthing.replace(word,'',1)
    x+=1

print x

演示:

>>> 
memory memory memory
3

您的问题 - 查找和计算字符串中所有出现的子字符串 - 不需要按定义进行替换。

您应该能够只接受输入,强制降低输入,并使用count方法查找子串。

num_substrings = input().lower().count(substring)

我还没有看到这种方法,只是循环查找计数出现:

a='this is a test aaaa'
count =0
loc = a.find('is')
while loc > -1:
    count += 1
    loc = a.find('is',loc+1)
print count -1 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM