這是我擁有的資料。
t = 'Billy and Willy and Billy and someone'
words = ['Billy', 'Willy', 'Billy']
我打算按順序找到單詞。首先我找到比利,然后我縮短行直到比利這個詞的結尾。
例如:
new_t = ' and Willy and Billy and someone'
然后我打算去找威利等等。
所以這里是我寫的:
t = 'Billy and Willy and Billy and someone'
words = ['Billy', 'Willy', 'Billy']
indexes = []
j = 0
for i in words:
l = re.search(i, t[j:]).span()
indexes.append(l)
j = l[1]
我知道我做錯了,但是你能幫我得到這樣的結果嗎:
Billy = (0,5)
Willy = (10,15)
Billy = (20,25)
uj5u.com熱心網友回復:
要查找確切的子字串,您不需要re. 您可以改為使用str.index:
t = 'Billy and Willy and Billy and someone'
words = ['Billy', 'Willy', 'Billy']
indexes = []
current_pos = 0
for word in words:
ind = t.index(word, current_pos)
indexes.append((ind, ind len(word)))
current_pos = ind 1
print(indexes) # [(0, 5), (10, 15), (20, 25)]
for w, i in zip(words, indexes):
print(w, '=', i)
# Billy = (0, 5)
# Willy = (10, 15)
# Billy = (20, 25)
的第二個引數index是搜索的起始位置,所以你只需要在current_pos搜索完成后更新起始位置()。
或者使用海象運算子(python 3.8 ),您可以將第二段縮短為
b = 0
indexes = [(a := t.index(w, b), b := a len(w)) for w in words]
uj5u.com熱心網友回復:
使用re:
import re
t = 'Billy and Willy and Billy and someone'
words = 'Billy', 'Willy'
for match in re.finditer('|'.join(words), t):
print(f"{match[0]} = {match.span()}")
Billy = (0, 5)
Willy = (10, 15)
Billy = (20, 25)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/341551.html
