本文介绍了如何在文本文件中找到重复的行并打印它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含大约 1,200 行的文本文件.其中一些是重复的.

I have a text file with some 1,200 rows. Some of them are duplicates.

如何在文件中找到重复的行(但不担心大小写),然后在屏幕上打印出该行的文本,以便我可以找到它?我不想删除它们或任何东西,只需找出它们可能是哪些行即可.

How could I find the duplicate lines in the file (but not worrying about case) and then print out the line's text on the screen, so I can go off and find it? I don't want to delete them or anything, just find which lines they might be.

推荐答案

这很容易使用:

with open('file') as f:
    seen = set()
    for line in f:
        line_lower = line.lower()
        if line_lower in seen:
            print(line)
        else:
            seen.add(line_lower)

这篇关于如何在文本文件中找到重复的行并打印它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 09:45