python - 结合多个标准
问题描述
我有以下三个长度不等的列表:
a = [2.13, 5.48,-0.58]
b = [4.17, 1.12, 2.13, 3.48,-1.01,-1.17]
c = [6.73, 8, 12]
d = [(2.13,2.13),(5.48,-1.17),(-0.58,4.17)]
e = [(4.17,12),(2.13,6.73)]
我需要创建一个组合_abc = [ (x,y,z) for x in a for y in b for z in c]使得 (x,y) 不等于 d 并且 (y,z) 不等于e
解决方案
如果我理解你是正确的,只需将 if 语句添加到你的列表理解中:
[(x, y, z) for x in a for y in b for z in c if (x, y) not in d and (y, z) not in e]
为了简单起见,您也可以使用itertools.product
:
from itertools import product
[(x, y, z) for x, y, z in product(a, b, c) if (x, y) not in d and (y, z) not in e]
推荐阅读
- html - 无论html中的窗口大小如何,如何将按钮的位置固定在中心
- reactjs - React Transition Group:当TransitionGroup子项位于单独的组件中时,退出转换不起作用
- javascript - 如何让代码等待完成 fileReader.onload() 事情?
- c++ - PIR 运动传感器的代码未使用 Blynk 显示任何输出
- kubernetes - 本地 Kubernetes 管理
- python - 如何创建一个干净的 postgres 数据库以使用 sqlalchemy 进行测试
- python - 如何使用 Python/Numba 对数组执行并行归约
- mysql - 汇总值后将返回记录限制为 5
- python - 为什么我的 MongoDB 没有连接到我的 python 程序?
- sql - 合并表 - Oracle