Python 尝试压缩句子,然后将其上载到文件
我有一个句子,我试图压缩它。然后我必须把它上传到一个外部文件。我的句子必须上传,我的压缩句子也必须上传到另一个文件中 这是我的节目Python 尝试压缩句子,然后将其上载到文件,python,python-2.7,python-3.x,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 =
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
对于“句子”->“这是一个句子,不是很长的句子”
请帮助,谢谢您的测试:
if word not in word_dictionary:
...
但是您从不在字典中保存任何单词,而是保存最高的计数器:
compressed.append(word_dictionary.setdefault(highest, new))
因此,
word
将永远不会出现在word\u字典中,而highest
将始终递增。这应该有效,我修改了您的原始代码并删除了highest
和word\u字典
,如果元素索引+1
出现在句子中不止一次,否则它会在列表中追加另一个中的最高数字,如果它的计数小于1,我还必须用0初始化另一个以避免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()
句子=“这是一个句子,不是很长的句子”
s=句子。拆分()
另一个=[0]
对于s中的i:
如果s.计数(i)<2:
另一个。追加(最大值(另一个)+1)
其他:
附加(s.index(i)+1)
另一个。删除(0)
fo=打开(“索引”,“w”)
对于另一个中的索引:
fo.write(str(索引))
fo.close()
fo=开放(“句子”,“w”)
fo.写(句子)
fo.close()
“请帮助”是什么?你的代码怎么了?它产生了什么与您的期望不符的结果?它将1,2,3,4,5,6,7,8,9,10,11保存到“索引”中,而不是“1,2,3,4,5,2,6,3,7,8,4”这听起来可能有点愚蠢……但我不确定您在这里想告诉我什么您期望1,2,3,4,5,2,6,3,7,8,4
作为输出的原因是“是第二个和第六个单词,因此2
位于第二个和第六个位置。要做到这一点,你必须“记住”你遇到的每个生词的第一个位置,你没有这样做。我觉得你还没有完全理解你要实现的压缩算法,也许在编码之前先试试纸笔的例子。