我有一个句子,我试图压缩它。然后我必须将其上传到外部文件。我的句子必须上传,而我的压缩句子在另一个文件中也一样。试图压缩一个句子,然后上传到一个文件
这是我的计划......
word_dictionary = {}
highest = 0
sentence = "This is a sentence and is not a very long sentence".split()
s= "This is a sentence and is not a very long sentence"
compressed = []
new = ""
for word in sentence:
if word not in word_dictionary:
highest += 1
compressed.append(word_dictionary.setdefault(highest, new))
print(word_dictionary)
word_dictionary = str(word_dictionary)
fo = open("index","a+")
fo.write(word_dictionary)
fo.close()
fo=open("sentence","a+")
fo.write(s)
fo.close()
我想上传到文件将是...
为 “指数” ---> 1,2,3, 4,5,2,6,3,7,8,4
为“句” --->“这是一个句子,是不是一个很长的句子”
请帮帮忙,谢谢
“请帮帮忙” _with what_?你有什么问题?它产生的结果与您的期望不符? – Chris
它将1,2,3,4,5,6,7,8,9,10,11保存为“索引”而不是“1,2,3,4,5,2,6,3,7, 8,4 –