【发布时间】:2021-01-28 23:37:50
【问题描述】:
我正在做一个练习,我必须从另一个字符串中删除所有出现的字符串,虽然它有效,但似乎程序正在迭代并且无限次,我无法修复它。我知道有一个 string.replace() 函数,但我想尝试在不使用该函数的情况下解决问题。 这是代码:
''' def remove_all(substr,theStr):
index = theStr.find(substr)
if index > 0:
newstr = ""
while index > 0:
sizsub = len(substr)
newstr = theStr[:index] + theStr[(index + len(substr)):]
index = newstr.find(substr)
return newstr
else:
return theStr
remove_all("an", "banana")
'''
错误消息:“TimeLimitError:程序超出运行时间限制。在第 9 行”
提前致谢。
【问题讨论】:
标签: python string while-loop