gpt4 book ai didi

python-3.x - 如何查找文件中重复的单词?

转载 作者:行者123 更新时间:2023-12-02 17:58:03 31 4
gpt4 key购买 nike

我的代码遇到一些问题。我试图在文件中查找重复的单词,例如“the the”,然后打印它发生的行。到目前为止,我的代码适用于行数,但给出了整个文件中重复的所有单词,而不仅仅是紧接着的单词。

我需要更改什么才能只计算双倍的单词?

my_file = input("Enter file name: ")
lst = []
count = 1
with open(my_file, "r") as dup:
for line in dup:
linedata = line.split()
for word in linedata:
if word not in lst:
lst.append(word)
else:
print("Found word: {""} on line {}".format(word, count))
count = count + 1
dup.close()

最佳答案

my_file = input("Enter file name: ")
with open(my_file, "r") as dup:
for line_num, line in enumerate(dup):
words_in_line = line.split()
duplicates = [word for i, word in enumerate(words_in_line[1:]) if words_in_line[i] == word]
# now you have a list of duplicated words in line in duplicates
# do whatever you want with it

关于python-3.x - 如何查找文件中重复的单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43186099/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com