0
sentence = input("Please enter a sentence:")
words = sentence.split()
position= [0]
myList = []
[myList.append(x) for x in words if x not in myList]
a =(" ".join(myList))
print (myList)
if words.count(i) <2:
position.append(max(position) +1)
else:
position.append(words.index(i) +1)
position.remove(0)
print (position)
words = str(words)
position = str(position)
file = open("recreated_positions","w")
file.write(a)
file.write(position)
file.close()
這段代碼將把每個單詞的位置作爲一個數字並將這個句子打印在python shell和textfile上。列入文本文件的字符串?
例如,如果句子是'我喜歡英語,但我不喜歡數學'。 該計劃將輸出
['i','like','english,'but','do','not','maths']
[1,2,3,4,1,5,6,2,7]
在文本文件,程序不輸出它 上面做了同樣的方式了一句。
有關如何使其在文本文件中做同樣的事情的任何建議?
修復格式。 – khajvah
那麼它在文本文件中輸出什麼? – UnholySheep