Here's a linear algorithm:
position = {} # word -> position
words = sentence.split()
for word in words:
if word not in position: # new word
position[word] = len(position) + 1 # store its position
print(*map(position.__getitem__, words), sep=",")
# -> 1,2,3,4,5,6,7,8,9,1,3,9,6,7,8,4,5
The print()
call uses Python 3 *
syntax to unpack the result returned by map()
that returns positions for the corresponding words here. See What does ** (double star) and * (star) do for parameters?