编写一个带有两个字符串参数的代码,并返回一个参数可以在另一个参数中找到的次数的整数计数[duplicate]

时间:2022-03-24 09:10:42

This question already has an answer here:

这个问题在这里已有答案:

I have to write a code substrn, which takes string arguments sup and sub, and returns the integer count of the (possibly overlapping) number of times sub can be found in sup.

我必须编写一个代码substrn,它接受字符串参数sup和sub,并返回可以在sup中找到sub的次数(可能重叠)的整数计数。

I wrote this code

我写了这段代码

def substrn(sup,sub):
   if sub in sup:
      x = 0
      for i in sup:
         if sub in sup[sup.index(i):(int(len(sub))+int(sup.index(i)))]:
            x = x +1 
      return x 
   else:
      return 0

print(substrn("wooloomooloo", "oo"))
print(substrn("wablabmablab", "ab"))

Can anyone help me understand what went wrong with my code and how I can change it? My first print statement produced 8 but my second print statement produced 4, they should be the same.

任何人都可以帮我理解我的代码出了什么问题,以及我如何改变它?我的第一个印刷声明产生了8个,但我的第二个印刷声明产生了4个,它们应该是相同的。

1 个解决方案

#1


-1  

Here try this instead:

在这里试试这个:

def substrn(sup,sub):
       # if sub in sup: # this is redundant since you are already checking 
      x = 0
      for i in range(len(sup)):
         if sub == sup[i:int(len(sub))+i]: 
         # try not to use a in b as this indicates partial search
         # so to make your logic more explicit you use a == b 
            sup[i:int(len(sub))+i]
            x = x +1 
      return x 


print(substrn("wooloomooloo", "oo")) # prints 4
print(substrn("wablabmablab", "ab")) # prints 4

The main problem in your initial code is this portion for i in sup: and sup.index(i).

初始代码中的主要问题是i的这一部分:sup:和sup.index(i)。

for i in sup: iterates through each letter in sup and sup.index(i) will only find the first occurrence of the index of i in sup. It does not care about where i comes from, but only what i is.

for i in sup:遍历sup中的每个字母,sup.index(i)只会在sup中找到i的索引的第一个出现。它不关心我来自哪里,而只关心我是什么。

If you actually printed out sup.index(i), you'll find that sup.index(i) will always be 1 for each loop, and hence when u execute substrn("wooloomooloo", "oo") it will always capture the first oo. Since you have 8 os, your substrn("wooloomooloo", "oo") will return 8

如果你实际打印出sup.index(i),你会发现每个循环的sup.index(i)总是1,因此当你执行substrn(“wooloomooloo”,“oo”)时,它将始终捕获第一个oo。由于你有8个os,你的substrn(“wooloomooloo”,“oo”)将返回8

I hope this helps :)

我希望这有帮助 :)

#1


-1  

Here try this instead:

在这里试试这个:

def substrn(sup,sub):
       # if sub in sup: # this is redundant since you are already checking 
      x = 0
      for i in range(len(sup)):
         if sub == sup[i:int(len(sub))+i]: 
         # try not to use a in b as this indicates partial search
         # so to make your logic more explicit you use a == b 
            sup[i:int(len(sub))+i]
            x = x +1 
      return x 


print(substrn("wooloomooloo", "oo")) # prints 4
print(substrn("wablabmablab", "ab")) # prints 4

The main problem in your initial code is this portion for i in sup: and sup.index(i).

初始代码中的主要问题是i的这一部分:sup:和sup.index(i)。

for i in sup: iterates through each letter in sup and sup.index(i) will only find the first occurrence of the index of i in sup. It does not care about where i comes from, but only what i is.

for i in sup:遍历sup中的每个字母,sup.index(i)只会在sup中找到i的索引的第一个出现。它不关心我来自哪里,而只关心我是什么。

If you actually printed out sup.index(i), you'll find that sup.index(i) will always be 1 for each loop, and hence when u execute substrn("wooloomooloo", "oo") it will always capture the first oo. Since you have 8 os, your substrn("wooloomooloo", "oo") will return 8

如果你实际打印出sup.index(i),你会发现每个循环的sup.index(i)总是1,因此当你执行substrn(“wooloomooloo”,“oo”)时,它将始终捕获第一个oo。由于你有8个os,你的substrn(“wooloomooloo”,“oo”)将返回8

I hope this helps :)

我希望这有帮助 :)