У меня есть список определенных слов
['to', 'with', 'in', 'for']
Я хочу создать функцию, которая принимает предложение, и если в моем списке есть слово, оно должно выбрать следующие два слова после него иПоместите их в список (мне это нужно для части моего генератора предложений). Например:
sentence = 'In the morning I went to the store and then to the restaurant'
Я хочу получить
['tothe', 'tostore', 'tothe', 'torestaurant']
Я написал этот код:
preps = ['to', 'with', 'in', 'for']
def nextofnext_words_ofnegs(sentence):
list_of_words = sentence.split()
next_word = []
for i in list_of_words:
for j in preps:
if i == j:
next_word.append(j + list_of_words[list_of_words.index(i) + 1])
next_word.append(j + list_of_words[list_of_words.index(i) + 2])
return next_word
Однако я получаю это:
['tothe', 'tostore', 'tothe', 'tostore']
Вместо этого:
['tothe', 'tostore', 'tothe', 'torestaurant']