如何在列表理解中排除多个参数?
我有一个简单的表达方式: data = [如果e不在x.thing中,则x为blob中的x]
然而,我想测试多个字符串而不是一个字符串'e' 所以有点伪代码:
exclude = ['tom', 'dick', 'harry', ....]
data = [x for x in blob if <any of the values of exclude> not in x.thing]
我不知道排除的长度或值,所以我不能这样做
[x for x in blob if e1 not in x.thing else x for x in blob if e2 not in x.thing else ... ]
答案 0 :(得分:0)
试试这个,只需复制粘贴这一行:
[x for x in blob if all(e not in x.thing for e in exclude)]
例如:
输入:
exclude = ['tom', 'dick', 'harry']
blob=['franceharry','germany','gerrytom']
[x for x in blob if all(e not in x for e in exclude)]
输出:
['germany']