用python调试列表

用python调试列表,python,Python,我有一个py脚本(由from提供)来调试两个字符串列表 现在我想修改它以调试列表并删除重复的字符串: filename_1 = 'A.txt' filename_2 = 'B.txt' filename_3 = 'C.txt' with open(filename_1, 'r') as f1, open(filename_2, 'r') as f2, open(filename_3, 'w') as fout: s = set(val.strip() for val in f1.read

我有一个py脚本(由from提供)来调试两个字符串列表


现在我想修改它以调试列表并删除重复的字符串:

filename_1 = 'A.txt'
filename_2 = 'B.txt'
filename_3 = 'C.txt'
with open(filename_1, 'r') as f1, open(filename_2, 'r') as f2, open(filename_3, 'w') as fout:
    s = set(val.strip() for val in f1.readlines())
    for row in f2:
        row = row.strip()
        if row not in s:
            fout.write(row + '\n')
名单内容:

 A.txt
 string1
 string2

 B.txt
 string1
 string3
预期结果:

 C.txt
 string1
 string2
 string3
谢谢

我是新来的,我道歉。我真正需要的是从列表A中删除B的内容。无论如何,谢谢

这是答案,经过研究(3个案例):

从A.txt列表中删除B.txt内容并退出到C.txt

a=set(line.strip().lower() for line in open('A.txt').readlines())
b=set(line.strip().lower() for line in open('B.txt').readlines())
open("C.txt", "w").write("\n".join(a.difference(b)))
比较A.txt和B.txt,并在C.txt中显示新行B.txt

a=set(line.strip().lower() for line in open('A.txt').readlines())
b=set(line.strip().lower() for line in open('B.txt').readlines())
open("C.txt", "w").write("\n".join(b.difference(a)))
将A.txt和B.txt的内容合并到C.txt中

a=set(line.strip().lower() for line in open('A.txt').readlines())
b=set(line.strip().lower() for line in open('B.txt').readlines())
open("C.txt", "w").write("\n".join(b | a))

文件的第一部分包含
f2
中不在
f1
中的项目,因此只需将
f1
的所有内容添加到结果中即可

with open(filename_1, 'r') as f1, open(filename_2, 'r') as f2, open(filename_3, 'w') as fout:
    s = set(val.strip() for val in f1.readlines())
    for row in f2:
        row = row.strip()
        if row not in s:
            fout.write(row + '\n')
    for row in s:
        fout.write(row + '\n')

等等,您尝试了什么,遇到了什么问题?删除重复的字符串从列表A和列表B中删除重复的字符串,然后将结果保存到列表C?如果是这样,为什么listC在listA和listB中重复时包含
string2
?@acaler看起来您处于堆栈溢出状态,让其他人为您编写代码。您没有努力去理解代码为什么工作。标记为我不宽恕这种行为,这不是堆栈溢出的原因。@Joshurali我很抱歉。我是新来的,不知道你的规矩。我不知道这类问题是被禁止的