I am developing a program that identifies individual words in a sentence, stores these in a list and replaces each word in the original sentence with the position of that word in the list, so the sentence can be recreated from the positions of these words in this list using the sequence 1,2,3,4,5,6,7,8,9,1,3,9,6,7,8,4,5
. My code so far is below but I need some advice on how to make it more efficient and shorter:
import time
sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY"
s = sentence.split()
another = [0]
time.sleep(0.5)
print(sentence)
for count, i in enumerate(s):
if s.count(i) < 2:
another.append(max(another) + 1)
else:
another.append(s.index(i) +1)
another.remove(0)
time.sleep(0.5)
print(another)