同时过滤两个列表

时间:2013-08-01 13:22:52

标签: python filtering

我有三个清单:

del_ids = [2, 4]
ids = [3, 2, 4, 1]
other = ['a', 'b', 'c', 'd']

我的目标是删除结果为

del_ids
ids = [3, 1]
other = ['a', 'd']

我试图为要保留的元素(mask = [id not in del_ids for id in ids])执行掩码,我计划在两个列表中应用此掩码。

但我觉得这不是一个pythonic解决方案。你能告诉我怎样才能做得更好吗?

4 个答案:

答案 0 :(得分:12)

压缩,过滤并再次解压缩:

ids, other = zip(*((id, other) for id, other in zip(ids, other) if id not in del_ids))

zip()个调用将每个id与相应的other元素配对,生成器表达式会筛选id列在del_ids中的任何对,然后zip(*..)将剩余的对再次梳理成单独的列表。

演示:

>>> del_ids = [2, 4]
>>> ids = [3, 2, 4, 1]
>>> other = ['a', 'b', 'c', 'd']
>>> zip(*((id, other) for id, other in zip(ids, other) if id not in del_ids))
[(3, 1), ('a', 'd')]

答案 1 :(得分:2)

zip,filter,unzip:

ids, other = zip(*filter(lambda (id,_): not id in del_ids, zip(ids, other)))

答案 2 :(得分:2)

为了避免学习棘手的语法,分两步完成。

other = [o for my_id, o in zip(ids, other) if my_id not in del_ids]
ids = [my_id for my_id in ids if my_id not in del_ids]

缺点
您必须以正确的顺序执行语句,因此如果由于某种原因顺序发生更改,则存在出现错误的风险。

优势
它很简单,所以下次你想这样做时不必搜索 Stackoverflow。

答案 3 :(得分:1)

转换为熊猫数据框并应用掩码:

del_ids = [2, 4]
ids = [3, 2, 4, 1]
other = ['a', 'b', 'c', 'd']
df = pd.DataFrame({'ids':ids,'other':other})
df = df[~df.ids.isin(del_ids)]
ids = df['ids'].tolist()
other = df['other'].tolist()