在python中从列表中删除多个重复值

2024-04-25 04:16:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在处理由报表软件生成的大型(约5000行)文本文件。这些文件每页都有多个标题行,并且有许多空行。我已经找到了一种方法来过滤掉我不需要的数据,但是我想知道这是否是最好的方法。我有这个函数,我用它来过滤列表,它基本上是遍历列表,并通过每次删除一个过滤行来减少它。你知道吗

def process_block(b):
    b1 = [line for line in b if not line.startswith('100   V')]
    b2 = [line for line in b1 if not line.startswith('300   V')]
    b3 = [line for line in b2 if not line.startswith('400   V')]
    b4 = [line for line in b3 if not line.startswith('AR00000')]
    b5 = [line for line in b4 if not line.startswith('734 - C')]
    b6 = [line for line in b5 if not line.lstrip().startswith('TXN DAT')]
    b7 = [line for line in b6 if not line.startswith('   ACCO')]
    b8 = [line for line in b7 if not line.rstrip() == '']
    return b8

我觉得我传球太多了。有没有更好的方法来完成这个过滤?你知道吗


Tags: 方法in列表foriflinenotb2
3条回答

您可能会发现这些方法很有用:

给出:

a = ['test', 'test_1', 'test_2', 'test_3', 'test']

b = ['test']

我们可以从a中减去b,如下所示:

c = list(set(a) - set(b))

print(c)

产生:

['test_3', 'test_2', 'test_1']

或者我们可以按如下方式删除重复项:

c = list(dict(zip(a, [None]*len(a))).keys())

print(c)

产生:

['test_3', 'test_2', 'test', 'test_1']

注意,在后一种方法中,顺序丢失。如果希望保留顺序,请使用Python本机库中的collections.OrderedDict。你知道吗

现在只需要拆分字符串并对其进行操作。你知道吗

你绝对可以一次完成。你知道吗


def process_block(b)
    return [line for line in b if  
        not line.startswith(
                ('100   V', '300   V', '400   V', 'AR00000', '734 - C', '   ACCO')
            )
        and not line.lstrip().startswith('TXN DAT')
        and not line.rstrip() == ''] 

^{}方法接受前缀元组。因此,您可以使用一个列表来代替多个循环,并将所有模式传递给一个startswith()方法。你知道吗

作为一种更为python的方法,您可以使用以下生成器函数从文件中返回迭代器过滤的对象:

def filter(file_name):
    prefixes = ("100   V", "300   V", "400   V",...)
    with open(file_name) as f:
        for line in f:
            if not line.lstrip().startswith(prefixes):
                yield line

如果不考虑内存使用,可以使用列表理解来过滤文件对象。你知道吗

filtered_obj = [line for line in file_object if not line.lstrip().startswith(prefixes)]

相关问题 更多 >