python中设置交集方法的使用方法

How to use the set intersection method in python

at_set = {'Num1', 'Num2', 'Num3'}
for files in os.listdir(zipped_trots_files):
    zipped_path = os.path.join(zipped_trots_files, files)
    with open(zipped_path, 'r') as output:
        reader = csv.reader(output, delimiter = '\t')
        for row in reader:
            read = [row for row in reader if row] 
            for row in read:
                if set(row).intersection(at_set):
                    print(row)

我猜我用错了交集函数...有人能看到吗?我试图只打印包含 Num1Num2Num3

的行

当我这样做时 print 我什么也没收到...

有重复的迭代。您需要删除过多的迭代或通过调用 output.seek(0).

返回到 reader 的开头
at_set = {'Num1', 'Num2', 'Num3'}
for files in os.listdir(zipped_trots_files):
    zipped_path = os.path.join(zipped_trots_files, files)
    with open(zipped_path, 'r') as output:
        reader = csv.reader(output, delimiter = '\t')
        for row in reader:
            if row and set(row).intersection(at_set):
                print(row)