尝试压缩句子然后将其上传到文件

时间:2016-02-11 18:07:19

标签: python python-2.7 python-3.x

我有一句话,我正在尝试压缩它。然后我必须将其上传到外部文件。我的句子必须上传,我的压缩句子也在上传。

这是我的计划......

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

代表“句子”---> “这是一个句子,并不是一个很长的句子”

请帮忙,谢谢

2 个答案:

答案 0 :(得分:1)

这应该有效,我修改了您的原始代码并移除了highestword_dictionary,如果它在句子中出现多次,则会附加元素index + 1,否则会附加列表another中的最大数字如果它的计数小于1,我还必须用{0}初始化another以避免max()为第一个元素抛出异常

sentence = "This is a sentence and is not a very long sentence"
s = sentence.split()
another = [0]

for i in s:
    if s.count(i) < 2:
        another.append(max(another) + 1)
    else:
        another.append(s.index(i) +1)

another.remove(0)

fo = open("index","w")
for index in another:
    fo.write(str(index))
fo.close()

fo=open("sentence", "w")
fo.write(sentence)
fo.close()

答案 1 :(得分:0)

你测试:

if word not in word_dictionary:
...

但你永远不会在字典中保存任何单词,而是保存highest计数器:

compressed.append(word_dictionary.setdefault(highest, new))

所以word永远不会在word_dictionary中,highest将永远增加。