2016-04-25 9 views
-1
sentence = raw_input("Enter a sentence: ") 
sentence = sentence.lower().split() 
uniquewords = [] 
for word in sentence: 
    if word not in uniquewords: 
     uniquewords.append(word) 

positions = [uniquewords.index(word) for word in sentence] 

recreated = " ".join([uniquewords[word] for word in positions]) 

positions = [x+1 for x in positions] 
print uniquewords 
print positions 
print recreated 

file = open('task2file1.txt', 'w') 
file.write('\n'.join(uniquewords)) 
file.close() 

file = open('task2file2.txt', 'w') 
file.write('\n'.join(positions)) 
file.close() 

これは、これまで私が持っているコードであり、すべてがテキストファイルに位置を保存する以外に動作し、私が取得エラーメッセージが保存番号

"file.write('\n'.join(positions)) 
TypeError: sequence item 0: expected string, int found" 
+1

は、Google検索にエラーメッセージを貼り付けてみましたか? – TigerhawkT3

答えて

2

は、文字列のリストにpositions変換であります。

file.write('\n'.join(str(p) for p in positions)) 
3

.join()メソッドは、文字列リストのみを結合できます。あなたは、文字列にあなたのpositionリストにint Sを変換する必要があります。

file.write('\n'.join(str(p) for p in positions)) 

または

file.write('\n'.join(map(str, positions))) 
+0

これは多くの助けになりました! – pythonprogrammer