在python文本文件的一句话中重复

在python文本文件的一句话中重复,python,python-3.8,Python,Python 3.8,嗨,我想写一个代码来读取一个文本文件,并用这个句子中有重复的单词来识别文件中的句子。我想把文件中的每一句话都放进字典里,找出哪些句子有重复。因为我是Python新手,所以在编写代码时需要一些帮助 这就是我到目前为止所做的: def Sentences(): def Strings(): l = string.split('.') for x in range(len(l)): print('Sentence', x + 1, ':

嗨,我想写一个代码来读取一个文本文件,并用这个句子中有重复的单词来识别文件中的句子。我想把文件中的每一句话都放进字典里,找出哪些句子有重复。因为我是Python新手,所以在编写代码时需要一些帮助

这就是我到目前为止所做的:

def Sentences():
    def Strings():
        l = string.split('.')

        for x in range(len(l)):
            print('Sentence', x + 1, ': ', l[x])

        return

    text = open('Rand article.txt', 'r')

    string = text.read()

    Strings()

    return

上面的代码将文件转换为句子。

假设您有一个文件,其中每一行都是一个句子,例如“句子.txt”:

策略可以是将句子拆分为组成词,然后使用
set
查找句子中的唯一词。如果生成的
集合
比所有单词的
列表
短,则您知道该句子至少包含一个重复单词:

sentences_with_dups = []
with open("sentences.txt") as fh:
    for sentence in fh:
        words = sentence.split(" ")
        if len(set(words)) != len(words):
            sentences_with_dups.append(sentence)

你能展示一下你在解决这个问题上的尝试吗?请展示一下你的尝试,让我们从那里开始。我也会用字典来解决这个问题。因此,看起来您需要阅读文件,将其拆分成句子,将每个句子拆分成单词,然后将这些单词放入词典中,以发现重复的单词(您也可以使用一套)。
sentences_with_dups = []
with open("sentences.txt") as fh:
    for sentence in fh:
        words = sentence.split(" ")
        if len(set(words)) != len(words):
            sentences_with_dups.append(sentence)