我已经创建了一个能正确识别重复单词的程序,但是我的操作方式不允许识别重复来自的行。我确实创建了一行行列表(行列表),然后从这些行中取出所有单词并将它们放入自己的列表中。我一直在寻找一种方法来显示重复来自哪一行。识别出的重复单词的打印行号
通过程序运行的文本可以在下面找到,然后是程序本身。忽略每个引号之后的空行,因为它没有出现在输入文本文件中。另外,作为参考,“XXX”标记是我希望显示行号的位置。
他会做他自己的自由的自由安全,
必须从防守的压迫,甚至他的敌人;
对于他是否违反了这一义务,他
,他建立了一个先例,将达到自己。
- 托马斯·潘恩
import math
file = open(str(input("Enter file name: ")), "r")
linelist = []
file_cont = file.readlines()
for lines in file_cont:
linelist.append(lines)
wordlist = []
# function that splits file into lines, then into words
def split_words(string):
lines = string
for line in lines:
for word in line.split():
yield word
# loop to add each word from prior function into a single list
for word in split_words(file_cont):
wordlist.append(word)
# variables declared
x = 0
y = 1
z = len(wordlist)
# loop that prints the first and following word next to each other
while z > x:
#print(wordlist[x], wordlist[y])
if wordlist[x] == wordlist[y]:
print("Found word: ",'"',wordlist[x],'"'," on line {}.".format(XXX), sep="")
x += 1
y += 1
if y == z:
break
任何帮助是极大的赞赏。谢谢!
是'He'一样'he'? –
不可以,不可以。不可以,不可以。不可以,不可以。不可以。不可以。不可以。不可以。 –