【问题标题】:Replacing duplicated words in python 3替换python 3中的重复单词
【发布时间】:2018-04-05 19:37:30
【问题描述】:

我想取一段如下所示的文本:

Engineering will save the world from inefficiency. Inefficiency is a blight on the world and its humanity.

然后返回:

Engineering will save the world from inefficiency..is a blight on the . and its humanity.

也就是说,我想删除重复的单词并将它们替换为“。” 这就是我开始编写代码的方式:

lines= ["Engineering will save the world from inefficiency.",
        "Inefficiency is a blight on the world and its humanity."]

def solve(lines):    
    clean_paragraph = []    
    for line in lines:    
        if line not in str(lines):
            clean_paragraph.append(line)
        print (clean_paragraph)    
        if word == word in line in clean_paragraph:
            word = "."              
     return clean_paragraph

我的逻辑是创建一个包含所有最差字符串的列表,并将每一个添加到一个新列表中,然后,如果该单词已经在列表中,则将其替换为“.”。我的代码返回 []。任何建议将不胜感激!

【问题讨论】:

  • 不应该返回。 “工程将拯救世界免于效率低下……是对……及其人性的一种破坏”。如果您要替换所有重复的单词
  • 没错,除了world这个词之外,the这个词也是重复的
  • 是的,应该。我不确定我的代码哪里出错了
  • @user8827983 它应该返回一个字符串或一个列表?

标签: python string python-3.x duplicates words


【解决方案1】:

另一种方法是

lines_test = 'Engineering will save the world from inefficiency. Inefficiency is a blight on the world and its humanity.'

text_array = lines_test.split(" ")
formatted_text = ''
for word in text_array:
    if word.lower() not in formatted_text:   
        formatted_text = formatted_text +' '+word
    else:
        formatted_text = formatted_text +' '+'.'

print(formatted_text)  

输出

Engineering will save the world from inefficiency. . is . blight on . . and its humanity.

【讨论】:

    【解决方案2】:

    这是执行此操作的一种方法。它用一个点替换所有重复的单词。

    lines_test = (["Engineering will save the world from inefficiency.",
                   "Inefficiency is a blight on the world and its humanity."])
    
    
    def solve(lines):
        clean_paragraph = ""
        str_lines = " ".join(lines)
        words_lines = str_lines.replace('.', ' .').split()
        for word in words_lines:
            if word != "." and word.lower() in clean_paragraph.lower():
                word = " ."
            elif word != ".":
                word = " " + word
            clean_paragraph += word
        return clean_paragraph
    
    
    print(solve(lines_test))
    

    输出:

    Engineering will save the world from inefficiency. . is . blight on . . and its humanity.
    

    在进行比较之前,将单词或字符串转换为小写或大写(一致形式)很重要。

    【讨论】:

      【解决方案3】:

      问题:

      if word == word in line in clean_paragraph:
      

      我不确定您对此有何期望,但它始终是False。这里有一些明确的括号是有益的:

      if word == ((word in line) in clean_paragraph):
      

      这会评估word in line,它可以是布尔值。但是,该值不会出现在clean_paragraph 的文本中,因此结果表达式为False

      修复

      编写您要编码的循环:

      for clean_line in clean_paragraph:
          for word in clean_line:
      

      此时,您必须弄清楚您想要的变量名称是什么。您尝试让几个变量同时代表两种不同的事物(lineword;我修复了第一个)。

      您还必须学会正确操作循环及其索引;问题的一部分是您一次编写的代码超出了您的处理能力——然而。备份,一次写一个循环,然后打印结果,这样你就知道你在做什么了。例如,以

      开头
      for line in lines:
      
          if line not in str(lines):
              print("line", line, "is new: append")
              clean_paragraph.append(line)
          else:
              print("line", line, "is already in *lines*")
      

      我想你会在这里发现另一个问题——甚至比我发现的问题还要早。解决这个问题,然后一次只添加一两行,逐步建立你的程序(和编程知识)。当某些东西不起作用时,您知道几乎可以肯定是新线路。

      【讨论】:

      • 谢谢!我现在遇到未定义单词的问题,但我不确定如何调用字符串中的各个单词。
      • @user8827983:正如我的更新所说,这是因为您尝试同时编写几个新概念。正如表达式所说,你“超越了你自己的腿”。慢慢来;如果您对如何定义变量感到困惑,请返回您的学习资料,或寻找当地的导师。这种不断增加的手动操作超出了 Stack Overflow 的目的范围。
      猜你喜欢
      • 2017-12-20
      • 1970-01-01
      • 1970-01-01
      • 2018-12-13
      • 2020-03-27
      • 1970-01-01
      • 2017-06-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多