Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if list items contains substrings from another list

Tags:

python

list

I have a lists:

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

and want to search for items that contain the string 'abc' and 'def' (and others in bad). How can I do that?

Almost same question here.

like image 738
Alex Avatar asked Jul 04 '12 12:07

Alex


People also ask

How do you check if a list contains items from another list?

There are 2 ways to understand check if the list contains elements of another list. First, use all() functions to check if a Python list contains all the elements of another list. And second, use any() function to check if the list contains any elements of another one.

How do you check if a string contains a list of substring?

The in Operator It returns a Boolean (either True or False ). To check if a string contains a substring in Python using the in operator, we simply invoke it on the superstring: fullstring = "StackAbuse" substring = "tack" if substring in fullstring: print("Found!") else: print("Not found!")

How do you check if a list contains a substring in Python?

Use any() function to check if a list contains a substring in Python. The any(iterable) with iterable as a for-loop that checks if any element in the list contains the substring and returns the Boolean value.


1 Answers

If you just want a test, join the target list into a string and test each element of bad like so:

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

From your question, you can test each element as a sub string against the each element of the other this way:

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

It is fast (in comparison to one of the other methods):

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

From your comment, here is how you get the elements that do not match:

>>> set(my_list)-{i for e in bad for i in my_list if e in i} {'ghi-789', 'qwe-111'} 
like image 135
dawg Avatar answered Oct 07 '22 09:10

dawg