2016-11-23 8 views
0

どのようにしてこのファイルを元のファイルを再現するために単語とリストのリストに圧縮するのですか?次に、元のファイルの圧縮されたファイルを取り出し、句読点や大文字を含む完全なテキストを再作成します。これをどのように把握するのですか?

startsentence = input("Please enter a sentence: ") 
sentence = (startsentence) 
a = startsentence.split(" ") 
dict = dict() 
number = 1 
positions = [] 
for j in a: 
    if j not in dict: 
     dict[j] = str(number) 
     number = number + 1 
    positions.append(dict[j]) 
print (positions) 


print(positions) 
f = open("postions.txt", "w") 
f.write(str(positions) + "\n" ) 
f.close() 

print(sentence) 
f = open("words.txt", "w") 
f.write(str(startsentence) + "\n" ) 
f.close() 
+0

質問がありますか? – DeepSpace

+0

申し訳ありませんが、私は間違って言いました、1秒。 –

+0

標準のPythonの 'dict'型を隠す変数名として' dict'を使用しないでください。 – AChampion

答えて

0

現在、あなただけのユニークワード全体startsentenceを書き出すとされていません。

f = open("words.txt", "w") 
f.write(str(startsentence) + "\n" ) 
f.close() 

あなただけのユニークな単語とそのインデックスを記述する必要があるとあなたは既にそれらと辞書を作成しました単語とそのインデックスdict(実際にはdictを変数名として使用しないでください。dctを使用します)。あなただけの(withステートメントを使用して)、その値に基づいてソートそれらを記述する必要があります。(:1よりも0からスタートする方がはるかに簡単ですBTW)とリスト

with open("words.txt", "w") as f: 
    f.write(' '.join(sorted(dct, key=dct.get)) + '\n') 

あなたは位置のリストを持っていると仮定すると、修復の単語は簡単です:

with open('positions.txt') as pf, open('words.txt' as wf: 
    positions = [int(p) for p in pf.read().split()] 
    words = wf.read().strip().split() 

recovered = ' '.join(words[p] for p in positions) # p-1 if you start from 1 
+0

ありがとうございましたこれは助けになりました。 –

関連する問題