如何比较 Python 中第一个文本文件的每一行与第二个文本文件的每一行?
How do I compare every line of the 1st text file to every line of the 2nd text file in Python?
我有 2 个名为 f1 和 f2 的文本文件,每个文件有 10 万行名称。我想将 f1 的第一行与 f2 的每一行进行比较,然后将 f1 的第二行与 f2 的每一行进行比较,依此类推。我已经尝试像下面的代码一样使用嵌套 for 循环,但它不起作用。
我做错了什么我似乎找不到?请问有人可以告诉我吗?
提前致谢。
old.txt
sourcreameggnest
saturnnixgreentea
saxophonedesertham
footballplumvirgo
soybeansthesting
cauliflowertornado
sourcreameggnest
saturnnixgreentea
new.txt
goldfishpebbleduck
saxophonedesertham
footballplumvirgo
abloomtheavengers
venisonflowersea
goodfellaswalker
saturnnixgreentea
代码:
with open('old.txt', 'r') as f1, open('new.txt', 'r') as f2:
for line1 in f1:
print('Line 1:- ' + line1, end='')
for line2 in f2:
print('Line 2:- ' + line2, end='')
if line1.strip() == line2:
print("Inside comparison" + line1, end='')
输出:
Line 1:- goldfishpebbleduck
Line 2:- sourcreameggnest
Line 2:- saturnnixgreentea
Line 2:- saxophonedesertham
Line 2:- footballplumvirgo
Line 2:- soybeansthesting
Line 2:- cauliflowertornado
Line 2:- sourcreameggnest
Line 2:- saturnnixgreentea
Line 1:- saxophonedesertham
Line 1:- footballplumvirgo
Line 1:- abloomtheavengers
Line 1:- venisonflowersea
Line 1:- goodfellaswalker
Line 1:- saturnnixgreentea
考虑到文件中的行数,我将完全避免嵌套循环 (O(n^2)
) 方法并将第二个文本文件的行加载到字典中(如果您关心行 and/or 这些行可以重复),或者在一组中。
然后我将遍历第一个文件中的行并检查它们是否在字典中并采取相应的行动。这将使用一些额外的 space 与第二个文件中的行数成线性关系,但将时间复杂度降低到 O(n)
,因为字典查找是常量。
至于您当前解决方案的不正确性,正如@Thierry Lathuille 所指出的,第二个迭代器在外循环的第一个 运行 之后耗尽,因此不会检查剩余的迭代。缓解措施是将文件的行读入一个列表,您可以在其中重复循环 (lines1 = f1.readlines(); lines2 = f2.readlines()
)。此外,如果您打算避免使用白色 space 行,则使用 strip
是不正确的。它们仍将作为空字符串进行比较,增加的缺点是剥离一行而不剥离另一行会产生不需要的差异。
无论如何,对于如此大的数字,二次时间复杂度的方法是不可行的。
您已经在第一个外循环后读取到文件末尾。顺便说一句,我不知道你可以循环打开一个文件。只需先存储行。另外我不明白你为什么只从其中一行中删除 '\n'。
with open('old.txt', 'r') as f1, open('new.txt', 'r') as f2:
lines1 = f1.readlines()
lines2 = f2.readlines()
for line1 in lines1:
print('Line 1:- ' + line1, end='')
for line2 in lines2:
print('Line 2:- ' + line2, end='')
if line1 == line2:
print("Inside comparison" + line1, end='')
结合@LukasNeugebauer 和@Thierry Lathuille 的答案,您的代码应该如下所示:
with open('old.txt', 'r') as f1, open('new.txt', 'r') as f2:
lines1 = f1.readlines()
lines2 = f2.readlines()
for line1 in lines1:
print('Line 1:- ' + line1, end='')
if line1 in lines2:
print("Inside comparison" + line1, end='')
如果您想知道使用 in
检查是否比遍历第二个列表并将每个值与 ==
进行比较更快,我测试了它。对于包含 10,000 行随机字符串的两个文件,用两个循环完全处理它们需要大约 2.8 秒,而使用 in
运算符只需要大约 0.8 秒。
如果你的文件不超过 1 兆字节,我不会真的费心优化它,否则你应该真正考虑你实际比较的是什么以及你可以使用哪些快捷方式。
编辑:
一些评论建议将第二行列表设为 set
,(将第 3 行更改为 lines2 = set(f2.readlines())
)这将使代码更快(我在上面使用的相同示例现在运行仅需 4 毫秒,>快 200 倍),但它实际上可能无法解决问题,因为将 list
转换为 set
将删除所有重复项,因此只有在确定可以丢弃重复项时才使用它。
with open('old.txt', 'r') as f1, open('new.txt', 'r') as f2:
lines2 = f2.readlines()
l2 = dict()
# fill dictionary for line2 with each name and the lines it occurs
for i in range(len(lines2)):
l2[lines[i]] += [i]
for line in f1.readlines():
if line in l2:
for j in l2[line]:
print(line, j, ...)
这两个循环中的每一个都应该有 O(n) 的复杂度,通过 in 的查找应该是 O(n)。
我有 2 个名为 f1 和 f2 的文本文件,每个文件有 10 万行名称。我想将 f1 的第一行与 f2 的每一行进行比较,然后将 f1 的第二行与 f2 的每一行进行比较,依此类推。我已经尝试像下面的代码一样使用嵌套 for 循环,但它不起作用。
我做错了什么我似乎找不到?请问有人可以告诉我吗?
提前致谢。
old.txt
sourcreameggnest
saturnnixgreentea
saxophonedesertham
footballplumvirgo
soybeansthesting
cauliflowertornado
sourcreameggnest
saturnnixgreentea
new.txt
goldfishpebbleduck
saxophonedesertham
footballplumvirgo
abloomtheavengers
venisonflowersea
goodfellaswalker
saturnnixgreentea
代码:
with open('old.txt', 'r') as f1, open('new.txt', 'r') as f2:
for line1 in f1:
print('Line 1:- ' + line1, end='')
for line2 in f2:
print('Line 2:- ' + line2, end='')
if line1.strip() == line2:
print("Inside comparison" + line1, end='')
输出:
Line 1:- goldfishpebbleduck
Line 2:- sourcreameggnest
Line 2:- saturnnixgreentea
Line 2:- saxophonedesertham
Line 2:- footballplumvirgo
Line 2:- soybeansthesting
Line 2:- cauliflowertornado
Line 2:- sourcreameggnest
Line 2:- saturnnixgreentea
Line 1:- saxophonedesertham
Line 1:- footballplumvirgo
Line 1:- abloomtheavengers
Line 1:- venisonflowersea
Line 1:- goodfellaswalker
Line 1:- saturnnixgreentea
考虑到文件中的行数,我将完全避免嵌套循环 (O(n^2)
) 方法并将第二个文本文件的行加载到字典中(如果您关心行 and/or 这些行可以重复),或者在一组中。
然后我将遍历第一个文件中的行并检查它们是否在字典中并采取相应的行动。这将使用一些额外的 space 与第二个文件中的行数成线性关系,但将时间复杂度降低到 O(n)
,因为字典查找是常量。
至于您当前解决方案的不正确性,正如@Thierry Lathuille 所指出的,第二个迭代器在外循环的第一个 运行 之后耗尽,因此不会检查剩余的迭代。缓解措施是将文件的行读入一个列表,您可以在其中重复循环 (lines1 = f1.readlines(); lines2 = f2.readlines()
)。此外,如果您打算避免使用白色 space 行,则使用 strip
是不正确的。它们仍将作为空字符串进行比较,增加的缺点是剥离一行而不剥离另一行会产生不需要的差异。
无论如何,对于如此大的数字,二次时间复杂度的方法是不可行的。
您已经在第一个外循环后读取到文件末尾。顺便说一句,我不知道你可以循环打开一个文件。只需先存储行。另外我不明白你为什么只从其中一行中删除 '\n'。
with open('old.txt', 'r') as f1, open('new.txt', 'r') as f2:
lines1 = f1.readlines()
lines2 = f2.readlines()
for line1 in lines1:
print('Line 1:- ' + line1, end='')
for line2 in lines2:
print('Line 2:- ' + line2, end='')
if line1 == line2:
print("Inside comparison" + line1, end='')
结合@LukasNeugebauer 和@Thierry Lathuille 的答案,您的代码应该如下所示:
with open('old.txt', 'r') as f1, open('new.txt', 'r') as f2:
lines1 = f1.readlines()
lines2 = f2.readlines()
for line1 in lines1:
print('Line 1:- ' + line1, end='')
if line1 in lines2:
print("Inside comparison" + line1, end='')
如果您想知道使用 in
检查是否比遍历第二个列表并将每个值与 ==
进行比较更快,我测试了它。对于包含 10,000 行随机字符串的两个文件,用两个循环完全处理它们需要大约 2.8 秒,而使用 in
运算符只需要大约 0.8 秒。
如果你的文件不超过 1 兆字节,我不会真的费心优化它,否则你应该真正考虑你实际比较的是什么以及你可以使用哪些快捷方式。
编辑:
一些评论建议将第二行列表设为 set
,(将第 3 行更改为 lines2 = set(f2.readlines())
)这将使代码更快(我在上面使用的相同示例现在运行仅需 4 毫秒,>快 200 倍),但它实际上可能无法解决问题,因为将 list
转换为 set
将删除所有重复项,因此只有在确定可以丢弃重复项时才使用它。
with open('old.txt', 'r') as f1, open('new.txt', 'r') as f2:
lines2 = f2.readlines()
l2 = dict()
# fill dictionary for line2 with each name and the lines it occurs
for i in range(len(lines2)):
l2[lines[i]] += [i]
for line in f1.readlines():
if line in l2:
for j in l2[line]:
print(line, j, ...)
这两个循环中的每一个都应该有 O(n) 的复杂度,通过 in 的查找应该是 O(n)。