文件位置和单词spli

2024-10-04 11:34:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个程序

  • 获取包含多个句子的文本文件
  • 询问用户是否要压缩/解压缩文件。在
  • 如果选择了Compress,句子将拥有所有唯一的单词和这些单词的位置,以重新创建句子。在
  • 如果选择解压缩,则需要找到压缩文本文件,并使用给定的位置列表和唯一的单词-解压缩-文件中的几个句子需要在单独的行上。在

这是我设法创建的代码。这是一个子程序,它有相当的缺陷。在

uniqueWords = []
positions = []
file = 

def valChoice():
    choice = (" ")
    while choice not in ["compress", "decompress"]:
        choice = input("Choose compress or decompress").lower()
        if choice not in ["compress", "decompress"]:
            print("Please input compress or decompress")
    finalChoice = valChoice()


if finalChoice = ("compress"):
    print("This where i get confused..")


elif finalChoice = ("decompress"):
    print("This where i get confused..")

这个代码有什么问题?我怎样才能修好它?在


Tags: or代码ininputifnot单词compress
1条回答
网友
1楼 · 发布于 2024-10-04 11:34:55

有了我的警告,我想我想你在问什么。在

迭代压缩输入。将每个单词的引用存储在字典中:单词本身是键,其位置是值。如果单词已经在字典中,则将新的位置引用添加到现有的引用列表中。在

解压的作用是相反的:把位置和单词排成一个序列。按升序排序。将单词连在一起形成原文。在

这就是你现在需要的帮助吗?在

相关问题 更多 >