根据列表字符串过滤列表

时间:2019-03-30 07:33:52

标签: python list indexing

我有两个列表(名称列表和路径列表),我想用它们来创建第三个列表,其元素是名称列表中不包含名称的路径:

names = ['name_1', 'name_2', 'name_3', 'name_4', 'name_5']
paths = ['dataset/name_1/00000003.jpg',
         'dataset/name_2/00000001.jpg', 
         'dataset/name_3/00000000.png', 
         'dataset/name_4/00000002.jpg',
         'dataset/name_5/00000000.jpg', 
         'dataset/name_6/00000002.jpg']

上面两个列表中的结果列表应该是

['dataset/name_6/00000002.jpg']

我是通过以下方式实现的:

check = []
for path in paths:
    exist_boolean = any([path.__contains__(x) for  x in names])
    check.append(exist_boolean)

check_final = [not i for i in check] 
list(compress(paths, check_final))

这是一个矫kill过正,有比我想出的丑陋逻辑更优雅的方法吗?

4 个答案:

答案 0 :(得分:2)

您可以使用列表理解:

[x for x in paths if all(y not in x for y in names)]

代码

names = ['name_1', 'name_2', 'name_3', 'name_4', 'name_5']
paths = ['dataset/name_1/00000003.jpg',
         'dataset/name_2/00000001.jpg', 
         'dataset/name_3/00000000.png', 
         'dataset/name_4/00000002.jpg',
         'dataset/name_5/00000000.jpg', 
         'dataset/name_6/00000002.jpg']

list3 = [x for x in paths if all(y not in x for y in names)]
# ['dataset/name_6/00000002.jpg']

答案 1 :(得分:1)

这是我的做法:)

names = ['name_1', 'name_2', 'name_3', 'name_4', 'name_5']
paths = ['dataset/name_1/00000003.jpg',
         'dataset/name_2/00000001.jpg',
         'dataset/name_3/00000000.png',
         'dataset/name_4/00000002.jpg',
         'dataset/name_5/00000000.jpg',
         'dataset/name_6/00000002.jpg']
newpath = paths
nopath = []
for path in paths:
    for name in names:
        if name in path:
            nopath.append(path)

for path in nopath:
    newpath.remove(path)

答案 2 :(得分:0)

在此处使用any函数;具有列表理解功能:-

>>> names = ['name_1', 'name_2', 'name_3', 'name_4', 'name_5']
>>> paths = ['dataset/name_1/00000003.jpg',
...          'dataset/name_2/00000001.jpg', 
...          'dataset/name_3/00000000.png', 
...          'dataset/name_4/00000002.jpg',
...          'dataset/name_5/00000000.jpg', 
...          'dataset/name_6/00000002.jpg']
>>> 
>>> [ j for j in paths if not any(n in j for n in names)]
['dataset/name_6/00000002.jpg']

答案 3 :(得分:0)

也许您想尝试以下方法之一

regex = re.compile(r'^(?!.*({}))'.format("|".join(names)))
list3 = list(filter(regex.search, paths))
# ['dataset/name_6/00000002.jpg']

# Perform test performance in list names 10k items and 15k items in paths
In[1]: %timeit list(filter(regex.search, paths))
15 ms ± 314 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [2]: %timeit [x for x in paths if all(y not in x for y in names)]
41.4 ms ± 903 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)