尝试了一堆代码,这是它得到的最后一行代码,但不是所有代码。正如标题所说,我正试图从第二个txt文件中找到唯一的行,并输出到一个新的txt文件中。
list1 = open('C:\1.txt','r',encoding='utf-8')
list2 = open('C:\2.txt','r',encoding='utf-8')
list_difference = []
for item in list2:
if item not in list1:
list_difference.append(item)
with open('some_output_file.txt','w',encoding='utf-8') as file_out:
for line in item:
file_out.write(line)```
要获得更快的解决方案,请将列表转换为集合:
list1 = open("file1.txt", "r", encoding="utf-8").readlines()
list2 = open("file2.txt", "r", encoding="utf-8").readlines()
set1 = set(list1)
set2 = set(list2)
print(f"len set1 {len(set1)}")
print(f"len set2 {len(set2)}")
# free the memory used by the lists
del list1
del list2
set_diff = set()
for item in set2:
if item not in set1:
set_diff.add(item)
print(f"len set_diff {len(set_diff)}")
print(set_diff)
校验item in set
在常数O(1(时间内运行,而对于列表则为O(n(。
干杯!
list1 = open('C:\1.txt','r',encoding='utf-8').readlines()
list2 = open('C:\2.txt','r',encoding='utf-8').readlines()
# .readlines() returns a list of each line in the file.
list_difference = []
for item in list2:
if item not in list1:
list_difference.append(item)
with open('some_output_file.txt','w',encoding='utf-8') as file_out:
for line in list_difference:
file_out.write(line)
这会将文本文件的每一行输出到相应的列表中。然后将这些唯一的行写入'some_output_file.txt'
。