python - 基于集合成员的过滤
问题描述
我会喜欢
small_evens = set([2, 4, 6, 8])
only_small_odds = filter(not in small_evens, [1, 3, 4, 1, 1, 2, 7, 6, 4, 5])
但当然这是一个语法错误。
两者都filter(lambda x: x not in set, items)
觉得[x for x in items if x not in set]
太冗长了。
还有其他方法吗?
我怀疑可能是因为,例如,较新的 Python 版本已经成为map(str.lower, strings)
可能(过去必须map(lambda s: s.lower(), strings)
)
解决方案
你可以这样做,这非常接近:
from itertools import filterfalse
only_small_odds = filterfalse(small_evens.__contains__, [1, 3, 4, 1, 1, 2, 7, 6, 4, 5])
推荐阅读
- python - Python XML 解析属性不可用
- flutter - Flutter,如何让一个 Listview.builder 锚定到底部
- c# - Windows 窗体中的 DataAnnotation 属性 - 如何在 WinForms 中使用 UIHint 属性
- java - 没有密钥库的 TLS 连接
- r - Dplyr 在不同的时间范围内分组百分比
- typescript - 如何类型检查未定义的联合类型属性访问
- ubuntu - 为 LXC 容器预留代理
- python - Tensorflow GCP 在线端点预测实例 Key 和 values 格式
- python - 无法使用 Selenium 下载多个文件
- angular - RXJS 6 - 在 .pipe 中包装 .map