如何使用多个条件过滤Python列表?

时间:2015-06-18 15:18:46

标签: python list

我有这个列表理解,如果它们不在列表lst_fcflds中,则返回列表RROPFields中的元素:

nfld_rrop = [i for i in lst_fcflds if i not in RROPFields]

并想要一个过滤器,这样如果OBJECTIDSHAPE在lst_fclfds中,它们也不会被返回 - 例如:

nfld_rrop = [i for i in lst_fcflds if i not in RROPFields and not in ["OBJECTID","SHAPE"]]

1 个答案:

答案 0 :(得分:5)

你只是缺少一个i

nfld_rrop = [i for i in lst_fcflds if i not in RROPFields and i not in ["OBJECTID","SHAPE"]]
                                                              ^

但是为了提高性能,我会首先添加一步来创建set,以便您可以更快地进行成员资格查找。

filters = set(RROPFields + ["OBJECTID", "SHAPE"])
nfld_rrop = [i for i in lst_fcflds if i not in filters]