Check if list items contains substrings from another list

后端 未结 4 1698
梦毁少年i
梦毁少年i 2020-11-29 09:28

I have a lists:

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

bad = [\'abc\', \'def\']

and want

4条回答
  •  南方客
    南方客 (楼主)
    2020-11-29 09:45

    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']
    

    or

    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']
    

提交回复
热议问题