我有以下三个不等长的列表:
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)]
我需要为a中的x创建一个combination_abc = [(x,y,z) 对于b中的y 对于c中的z] ,使得(x,y)不等于d并且(y,z)不等于e
答案 0 :(得分:3)
如果我了解您的正确要求,只需将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]