检查列表项是否包含其他列表中的项

时间:2012-07-04 12:16:53

标签: python list

我有一个清单:

my_list = ['abc-123', 'def-456', 'ghi-789', 'abc-456', 'def-111', 'qwe-111']

bad = ['abc', 'def']

并希望搜索包含字符串'abc'和'def'(以及其他错误字符串)的项目。我怎么能这样做?

几乎相同的问题here

4 个答案:

答案 0 :(得分:32)

如果您只想进行测试,请将目标列表加入字符串并测试bad的每个元素,如下所示:

>>> my_list = ['abc-123', 'def-456', 'ghi-789', 'abc-456', 'def-111', 'qwe-111']
>>> bad = ['abc', 'def']
>>> [e for e in bad if e in '\n'.join(my_list)]
['abc', 'def']

根据您的问题,您可以通过这种方式将每个元素作为子字符串对照另一个元素:

>>> [i for e in bad for i in my_list if e in i]
['abc-123', 'abc-456', 'def-456', 'def-111']

速度很快(与其他方法相比):

>>> def f1():
...    [item for item in my_list if any(x in item for x in bad)]
... 
>>> def f2():
...    [i for e in bad for i in my_list if e in i]
... 
>>> timeit.Timer(f1).timeit()
5.062238931655884
>>> timeit.Timer(f2).timeit()
1.35371994972229

根据您的评论,以下是获取不匹配元素的方法:

>>> set(my_list)-{i for e in bad for i in my_list if e in i}
{'ghi-789', 'qwe-111'}

答案 1 :(得分:10)

In [4]: filter(lambda item: any(x in item for x in bad), my_list)
Out[4]: ['abc-123', 'def-456', 'abc-456', 'def-111']

In [13]: [item for item in my_list if any(x in item for x in bad)]
Out[13]: ['abc-123', 'def-456', 'abc-456', 'def-111']

答案 2 :(得分:2)

some_list = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
bad = ['abc', 'def']
for s in some_list:
    for item in bad:
       if item in s:
          print 'Found ', s

这很简单,工作得很好而且速度很快(只有你的清单不是很大。)

答案 3 :(得分:0)

some_list=['abc-123', 'def-456', 'ghi-789', 'abc-456']
bad = ['abc', 'def']
for i in range (0,len(bad)):
    if bad[i] in some_list:
        print('Found a bad entry:', bad[i])