简体   繁体   中英

Python remove elements from a list that contains substrings from other list

I have two lists, and I want to remove the elements on a list l1 that have any different substring not mentioned in l2 . My lists are the following:

l1 = ['PC||macOS||Xbox One||PlayStation 4||Nintendo Switch',
 'macOS||PC||Xbox One',
 'iOS',
 'PC||PlayStation 4',
 'PC',
 'Web',
 'PC||macOS',
 'PC||macOS||Linux',
 'PC||Linux',
 'PC||Web',
 'PC||Xbox One||PlayStation 4||Nintendo Switch',
 'PC||macOS||Linux||Web',
 'macOS||PC||Linux||PlayStation 4||Xbox One',
 'PC||Android',
 'PC||macOS||Linux||Android',
 'macOS||iOS||Linux||PC||PlayStation 4',
 'Linux',
 'PC||macOS||Web',
 nan,
 'Xbox One||PC||Nintendo Switch||PlayStation 4',
 'iOS||PC||macOS||Linux',
 'PC||macOS||Android||Web',
 'iOS||Linux||PC||macOS',
 'Android',
 'macOS||PC||Linux',
 'Linux||PC'
]

l2 = ['PC', 'macOS', 'Linux', 'mac', 'Web']

What I want to obtain is all combinations in l1 that ONLY have the substrings stated in l2 . Therefore, in the new list I won't have any element with words like "Playstation 4" or "iOS" or "Xbox One". Something like:

l3 = [
 'PC',
 'Web',
 'PC||macOS',
 'PC||macOS||Linux',
 'PC||Linux',
 'PC||Web',
 'PC||macOS||Linux||Web',
 'PC||macOS||Linux||Android',
 'Linux',
 'PC||macOS||Web',
 'macOS||PC||Linux',
 'Linux||PC'
]

Make l2 a set for fast lookup then use all with a generator comprehension:

l2_set = set(l2)
l3 = [x for x in l1 if all(chunk in l2_set for chunk in x.split("||"))]

using sets is pretty easy.

l3 = [v for v in l1 if set(v.split('||')) <= set(l2)]

gonna have to filter out that errant nan though...

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