An easy solution is to get the create a version of the text without duplicates, but maintaining the same order, and finding indexes of the words in the original text from that list using index()
:
Create a list from the string by splitting by spaces:
text="the sailor went to sea sea sea to see what he could see see see but all that he could see see see was the bottom of the deep blue sea sea sea"
listText=text.split(" ")
Create new list without duplicates containing all words in text, using count()
to check word has not appeared previously:
unique_text=[listText[x] for x in range(len(listText))if listText[:x].count(listText[x])<1]
Use list comprehension to get index of every word in listText
in unique_text(and add 1):
positions=[unique_text.index(x)+1 for x in listText]
Final code:
text="the sailor went to sea sea sea to see what he could see see see but all that he could see see see was the bottom of the deep blue sea sea sea"
listText=text.split(" ")
unique_text=[listText[x] for x in range(len(listText))if listText[:x].count(listText[x])<1]
positions=[unique_text.index(x)+1 for x in listText]
Output:
[1, 2, 3, 4, 5, 5, 5, 4, 6, 7, 8, 9, 6, 6, 6, 10, 11, 12, 8, 9, 6, 6, 6, 13, 1, 14, 15, 1, 16, 17, 5, 5, 5]