2016-09-13 81 views
-4
sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY" 
s = sentence.split() 
another = [0] 
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) 
print(another) 

回答

0

我猜你想把句子放到文本文件中?如果是這樣,這裏是代碼:

text_file = ("textfile.txt", "w") 
text_file.write(sentence) 
text_file.close() 

請確保textfile.txt與您的程序在同一個文件夾中。