简体   繁体   中英

If list1 has any items of list2

Assume we have the following two lists,

list1 = ['text_svm_a', 'football_04', 'nice_sensor']

list2 = ['svm', 'sensor']

filtered_list = [item for item in list1 if item_contains_any_of_items_in_list2]

any help on writing item_contains_any_of_items_in_list2 is really appreciated.

Note: Both lists could be large so I don't want to hard code each condition.

You can use any :

filtered_list = [item for item in list1 if any(x in item for x in list2)]
# ['text_svm_a', 'nice_sensor']

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