To get a list of word positions in sentence
and recreate the original sentence from this list:
sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY"
s = sentence.split()
positions = [s.index(x)+1 for x in s]
recreated = [s[i-1] for i in positions]
# the reconstructed sentence
print(" ".join(recreated))
# the list of index numbers/word positions
print(positions)
# the positions as a space separated string of numbers
print(" ".join(positions)
Lists are zero-indexed so the first element is index 0, not 1. You could, of course, add 1 to all indices in the list comprehension if you wanted it to start at 1.
To get exactly the same output as your script produces:
sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY"
s = sentence.split()
positions = [s.index(x)+1 for x in s]
print(sentence)
print(positions)
Output:
ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY
[1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 3, 9, 6, 7, 8, 4, 5]