简体   繁体   中英

python find if each element of a string list contains any of another string list

suppose I have two string list:

a=['ab','ac','ad']
b=['abcd','baa','bacd','bbaa']

and I want to know if each element of list b has any of strings in a as its substring. The correct result should be: [True,False,True,False]. How do I code this?

You can use built-in function any within a list comprehension:

>>> [any(i in j for i in a) for j in b]
[True, False, True, False]

Something like:

[any([i in j for i in a]) for j in b]

would do the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM