私は文中の個々の単語を特定し、その単語の位置を出力するプログラムを作成しています。たとえば、文は:出力リスト内のリスト、文章、位置を含むプログラムにぶつかります
"私は
1 2 3 4 4 1 2をコードするのが大好きです" すべきである "私は私が愛するCODE CODE TO LOVE"
私は数字/ポジションを行うことができますが、私は個々の言葉を特定することができません。私を助けることができますか?私のコードを改善するのにも役立つでしょうか?ありがとう。私は今、この構造は@hsfzxjyが書いたものと非常に類似していることを実感
sentence = input("What is your sentence?: ")
sentence = sentence.split()
positions = [0]
for count, i in enumerate(sentence):
if sentence.count(i) < 2:
positions.append(max(positions) + 1)
else:
positions.append(sentence.index(i) +1)
positions.remove(0)
file = open("positions.txt","w")
positions = " ".join(map(str, positions))
file.write(positions)
file.close()
私は[コードレビューSE](http://codereview.stackexchange.com)でこれを掲示することをお勧めいたします。 –