【问题标题】:How do I check a text file line-by-line to detect if there are duplicates? [duplicate]如何逐行检查文本文件以检测是否有重复? [复制]
【发布时间】:2016-05-27 17:00:31
【问题描述】:

我试图让我的函数通过 Insults.txt 上的排序文本并确定是否有重复项,如果有则返回 false,但我似乎无法让它工作。我只是想检测重复项,而不是删除它们!有谁知道我做错了什么?

def checkInsultsFile(numInsults=1000, file="Insults.txt"):
    filename = open(file,'r').readlines()
    for i in range(0, numInsults):
        if [i] == [i+1]:
            return False
        else:
            return True

【问题讨论】:

  • 这需要minimal reproducible example。 “不工作”是什么意思?
  • 好点 Morgan - 当我运行代码时,即使文件中有重复项,也会返回 True。
  • 好吧,现在,只要它检查第一行,它就会返回。所以它只检查第一行。
  • 这里甚至没有检查文件中的任何内容 [i] == [i+1] 总是错误的。它会在第一次检查中返回。
  • 非常感谢大家的反馈 - 你知道我该如何解决这个问题吗?至于另一篇文章,我看到了,但我不是要替换或删除重复的行,而是要检测它们!!

标签: python


【解决方案1】:

试试这个,我不知道你为什么有 numInsults

def checkInsultsFile(numInsults=1000, file="Insults.txt"):
    lines = open(file, 'r').readlines()

    dict = {}

    for line in lines:
            dict[line] = dict.get(line,0) + 1

    for k,v in dict.iteritems():
            if v > 1:
                    return True
    return False

【讨论】:

    【解决方案2】:

    我的方法比较懒,一旦发现重复就会停止执行。

    def checkInsultsFile(filename):
        with open(filename, 'r') as file:
            s = set()
            for line in file:
                if line in s:
                     return True
                s.add(line)
            return False
        except IOError:
            handleExceptionFromFileError()
    

    【讨论】:

      【解决方案3】:

      如果你想检查整个文件,如果行数大于 1K,我也不确定你为什么要限制 numInsults。

      def checkInsultsFile(file):
          with open(file, 'r') as f:
              lines = [line.strip() for line in f] #puts whole file into list if it's not too large for your RAM
          check = set(lines)
          if len(lines) == len(check):
               return False
          elif len(check) < len(lines):
               return True
      
      checkInsultsFile("Insults.txt")
      

      替代方案(逐行遍历文件):

      def checkInsultsFile(file):
          lines = []
          with open(file, 'r') as f:
              for line in f:
                   lines.append(line.strip()) 
      
          check = set(lines)
          if len(lines) == len(check):
               return False
          elif len(check) < len(lines):
               return True
      
      checkInsultsFile("Insults.txt")
      

      此函数会将 Insults.txt 中的所有行放入一个列表中。 'Check' 是一个集合,它只会在 'lines' 列表中保留唯一的项目。如果行列表等于检查列表,则没有重复项,并返回 False。如果检查列表小于行列表,您就知道有重复,并且将返回 True。

      或者,您可以使用 bash(不知道您的操作系统)。只是指出有更快/更简单的方法可以做到这一点,除非您的 python 脚本将以其他方式利用文件中的唯一侮辱列表:

      排序 Insults.txt | uniq -c

      这类似于您可以在 Python 中对集合中的 Counter 执行的操作,这将为您提供文件中所有行的计数。

      【讨论】:

      • 我认为这个答案有一定的潜力,但并不完全存在。您为什么要注意在 python 问题中使用 bash 可以做什么?关于那个柜台东西的任何链接?是否真的有必要在可迭代时预先分配整个文件?文件已排序...有帮助吗?为什么需要最后一个 elsif,集合是否有可能大于 比列表?也许你不需要解决那些迂腐的事情(甚至大多数),但更多的细节会很好
      【解决方案4】:

      发生了什么

      if [i] == [i+1]:
          return False
      else:
          return True
      

      最初,i0。包含0 的单元素列表是否等于包含1 的单元素列表?显然不是。所以执行到else子句,函数返回True

      它甚至不关心文件的长度或内容,只要它存在并且可读。

      可行的解决方案

      itertools recipe 中获取pairwise(<em>iterable</em>) 的提示,它产生(line1, line2)(line2, line3)(line3, line4) 等对。

      另外,使用any() 函数来简化内部循环。

      from itertools import tee
      
      def any_consecutive_duplicate_lines(file='Insults.txt'):
          """Return True if the file contains any two consecutive equal lines."""
          with open(file) as f:
              a, b = tee(f)
              next(b, None)
              return any(a_line == b_line for a_line, b_line in zip(a, b))
      

      【讨论】:

        【解决方案5】:

        如果有骗子需要退货,我们可以把你的功能简单一点:

        def checkdup(file = "insults.txt")
          lines = open(file, 'r').readlines()
          return len(lines) != len(set(lines))
        

        基本上我们做两件事:将txt中的所有行做成一个列表,检查该列表中的项目数

        len(lines) #the number of insults in your file.
        

        与该列表的唯一元素集合中的项目数相同

        len(set(lines)) # the number of unique elements of our list, or unique insults
        

        如果不一样,肯定有骗子!

        【讨论】:

          猜你喜欢
          • 2012-01-09
          • 2016-05-01
          • 2018-10-29
          • 2011-09-18
          • 2018-09-27
          • 2013-12-25
          • 2012-01-25
          相关资源
          最近更新 更多