我试图遍历一个字符串并找到某个子字符串的所有出现,例如:
contant = "old men old men old men"
def this (l,n):
while n < 20:
m = contant[l:].index("o")
l = m + 3
n += 1
print(m,l)
this(0,1)
这只会一次又一次地返回nnumbers 0 3和5 8,而不是遍历整个字符串。
答案 0 :(得分:0)
尝试一下
value = "old men old men old men"
location = -1
# Loop while true.
while True:
# Advance location by 1.
location = value.find("o", location + 1)
# Break if not found.
if location == -1: break
# Display result.
print(location)
输出
0
8
16