[英]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
.我有两个列表,我想删除列表l1
中具有l2
中未提及的任何不同 substring 的元素。 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
.我想要获得的是l1
中只有l2
中所述的子字符串的所有组合。 Therefore, in the new list I won't have any element with words like "Playstation 4" or "iOS" or "Xbox One".因此,在新列表中,我不会有任何带有“Playstation 4”或“iOS”或“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
成为快速查找的集合,然后将all
与生成器理解一起使用:
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...不过,我必须过滤掉那个错误的南……
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.