簡體   English   中英

獲取單詞的開始和結束索引?

[英]Get start and end index of word?

我想獲取文本中單詞的開始索引和結束索引:作為輸入,我有:

text= "Hello, this red car is very beautiful and nice. Also, this car is green."
words_list= ["car" , "red","green"]

我想獲取文本中這些單詞的開始索引和結束索引..

import re

text= "Hello, this red car is very beautiful and nice. Also, this car is green."
words_list= ["car" , "red","green"]

for word in words_list:
    for m in re.finditer(word,text):
        print (m.group(),m.start(), m.end())

output:

car 16 19
car 59 62
red 12 15
green 66 71

或與:

print ("key:",m.group(),"start:",m.start(),"end:",m.end())

Output:

key: car start: 16 end: 19
key: car start: 59 end: 62
key: red start: 12 end: 15
key: green start: 66 end: 71

雖然 Synthase 是正確的,但還有一種方法可以在不導入任何模塊的情況下做到這一點。 我會這樣做:

text= "Hello, this red car is very beautiful and nice. Also, this car is green."
words_list= ["car" , "red","green"]


for word in words_list:
    if word in text:
        start_index = text.find(word)
        end_index = start_index + len(word)
        print(f"Word: {word}\nStart: {start_index}\nEnd: {end_index}\n")

這輸出:

Word: car
Start: 16
End: 19

Word: red
Start: 12
End: 15

Word: green
Start: 66
End: 71

代碼實際上非常簡單。

第 1 行和第 2 行定義了我們的文本和數組。 第 3 行開始了一個 for 循環,該循環針對單詞列表中的每個元素進行循環。 第 4 行檢查單詞是否在文本中。 如果是,則第 5 行獲取起始索引。 第 6 行通過將單詞的長度添加到開始索引來獲取結束索引。 最后,第 7 行以簡潔的格式打印信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM