请修改此代码。我正在尝试使用string.find()来查找给定范围内的所有情况(i到j)
def multi_find(seq, subseq, i, j):
list = []
while i != -1 and i < j:
list.append(seq.find(subseq,i,j))
i = seq.find(subseq,i,j) + 1
return list
print multi_find(seq, subseq, i, j)
答案 0 :(得分:0)
def multi_find(seq, subseq, i, j):
lst = []
loc = seq.find(subseq, i, j) # location of subseq
while loc != -1:
lst.append(loc)
i = loc + 1
loc = seq.find(subseq, i, j)
return lst
我认为使用i
,j
作为搜索seq
的范围并使用其他变量loc
来保留搜索结果更清晰。< / p>
答案 1 :(得分:0)
这样的事情可能更接近你想要做的事情。
import sys
def multi_find(seq, subseq, i, j):
list = []
for x in xrange(j - i):
temp = seq.find(subseq, x + i, j)
if(temp != -1):
list.append(temp)
x += sys.getsizeof(subseq)
else:
x += 1
return list
它运行广告会返回作为子字符串的令牌数,但我不太确定你想如何递增你的i。