首页 > 解决方案 > 结合多个标准

问题描述

我有以下三个长度不等的列表:

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

标签: pythonpython-3.xconditional-statementscombinations

解决方案


如果我理解你是正确的,只需将 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]

推荐阅读