2016-11-16 3 views
-1
sentence = input('Please enter your sentence: ') 
words = sentence.split() 

positions = {word:index for index, word in reversed(list(enumerate(words, 1)))} 

print(' '.join(str(positions.get(word)) for word in words)) 

答えて

0

質問は一般的ではありません。私は「ファイルをPythonで書く方法」であると考えています。また、ここで質問する前にドキュメントを読むこともできます。

https://docs.python.org/2/tutorial/inputoutput.html

with open('filename', 'w') as the_file: 
    the_file.write(whatever) 
+0

私も私の質問への答えを探しています、あなたは助けることができるチャンスがあります。 –

0
f = open('out.txt', 'w') 
f.write('...\n') 
f.close() 

この情報はHow to redirect 'print' output to a file using python? から来た;)

f.open=('PathToFile', 'w') 
sentence = input('Please enter your sentence: ') 
    words = sentence.split() 
    positions = {word:index for index, word in reversed(list(enumerate(words, 1)))} 
    f.write(' '.join(str(positions.get(word)) for word in words)) 
f.close 
+0

私はすでにこれを試しましたが、私のコードでどのように言いたいのか分かりません。 –

+0

maybe sentence = input( 'あなたの文章を入力してください:') words = sentence.split() positions = {word:インデックスの逆順の単語(list(enumerate(words、1))}}} f.write( '' .join(str(positions.get(word)words in words)) – Jesse

+0

私の答えのコードを変更しました。 – Jesse

関連する問題