Python 如何生成无重复和对称的itertools产品?
我知道有一个非常类似的问题:Itertools产品没有重复的副本 但我有不同的想法。例如:Python 如何生成无重复和对称的itertools产品?,python,combinations,product,itertools,Python,Combinations,Product,Itertools,我知道有一个非常类似的问题:Itertools产品没有重复的副本 但我有不同的想法。例如: a = ['add', 'sub'] b = [2,3, 'a', 'b'] c = [1,3, 'a', 'c'] list(it.product(a, b, c)) # result is:[('add', 2, 1),('add', 2, 3),('add', 2, 'a'), ('add', 2, 'c'), #('add', 3, 1),('add', 3, 3),('add', 3, 'a'
a = ['add', 'sub']
b = [2,3, 'a', 'b']
c = [1,3, 'a', 'c']
list(it.product(a, b, c)) # result is:[('add', 2, 1),('add', 2, 3),('add', 2, 'a'), ('add', 2, 'c'),
#('add', 3, 1),('add', 3, 3),('add', 3, 'a'),('add', 3, 'c'),('add', 'a', 1),('add', 'a', 3),
#('add', 'a', 'a'),('add', 'a', 'c'), ('add', 'b', 1),('add', 'b', 3),('add', 'b', 'a'),
#('add', 'b', 'c'),('sub', 2, 1),('sub', 2, 3),('sub', 2, 'a'),('sub', 2, 'c'),('sub', 3, 1),
#('sub', 3, 3),('sub', 3, 'a'),('sub', 3, 'c'),('sub', 'a', 1),('sub', 'a', 3),('sub', 'a', 'a'),
#('sub', 'a', 'c'),('sub', 'b', 1),('sub', 'b', 3),('sub', 'b', 'a'),('sub', 'b', 'c')]
对于结果:
我不想添加(a,a),因为第一个值==第二个值
我只想保留add(3,a)
,add(a,3)
中的一个,因为它是对称的
我的示例只包含两个列表,但我可以使用5个或更多列表来生成产品
我不能使用组合
,因为:
产品(['add',sub',1,2,3',a','b',c',repeat=3)
与产品(['add',sub',2,3',a',b',[1,3',a',c'])不同。
产品中的某些内容(['add','sub',1,2,3',a','b',c',repeat=3)
对我来说不合适
我想要一个快速的方法,因为我的程序是时间敏感的。IIUC,您可以:
from itertools import product, combinations, chain
a = ['add', 'sub']
b = [2, 3, 'a', 'b']
c = [1, 3, 'a', 'c']
for operation, operands in product(a, combinations(set(chain(b, c)), r=2)):
print((operation, *operands))
输出
('add', 1, 2)
('add', 1, 3)
('add', 1, 'a')
('add', 1, 'b')
('add', 1, 'c')
('add', 2, 3)
('add', 2, 'a')
('add', 2, 'b')
('add', 2, 'c')
('add', 3, 'a')
('add', 3, 'b')
('add', 3, 'c')
('add', 'a', 'b')
('add', 'a', 'c')
('add', 'b', 'c')
('sub', 1, 2)
('sub', 1, 3)
('sub', 1, 'a')
('sub', 1, 'b')
('sub', 1, 'c')
('sub', 2, 3)
('sub', 2, 'a')
('sub', 2, 'b')
('sub', 2, 'c')
('sub', 3, 'a')
('sub', 3, 'b')
('sub', 3, 'c')
('sub', 'a', 'b')
('sub', 'a', 'c')
('sub', 'b', 'c')
IIUC,你可以做:
from itertools import product, combinations, chain
a = ['add', 'sub']
b = [2, 3, 'a', 'b']
c = [1, 3, 'a', 'c']
for operation, operands in product(a, combinations(set(chain(b, c)), r=2)):
print((operation, *operands))
输出
('add', 1, 2)
('add', 1, 3)
('add', 1, 'a')
('add', 1, 'b')
('add', 1, 'c')
('add', 2, 3)
('add', 2, 'a')
('add', 2, 'b')
('add', 2, 'c')
('add', 3, 'a')
('add', 3, 'b')
('add', 3, 'c')
('add', 'a', 'b')
('add', 'a', 'c')
('add', 'b', 'c')
('sub', 1, 2)
('sub', 1, 3)
('sub', 1, 'a')
('sub', 1, 'b')
('sub', 1, 'c')
('sub', 2, 3)
('sub', 2, 'a')
('sub', 2, 'b')
('sub', 2, 'c')
('sub', 3, 'a')
('sub', 3, 'b')
('sub', 3, 'c')
('sub', 'a', 'b')
('sub', 'a', 'c')
('sub', 'b', 'c')
add(3,a)
和add(a,3)
是对称的,但是对于'sub'
(假设这些是已知的函数)是否也是对称的?@Tomerikoo是的,我认为它们是相同的,我正在尝试自动生成有意义的表达式。sub(a,2)和sub(2,a)对我来说是一样的。因为它没有提供额外的意义。谢谢你编辑了这个问题,我的语法不好你能有('add','c',2)
?add(3,a)
和add(a,3)
是对称的,但是'sub'
是否也有相同的作用(假设这些是已知的函数)?@Tomerikoo是的,我认为它们是相同的,我正在尝试自动生成有意义的表达式。sub(a,2)和sub(2,a)对我来说是一样的。因为它没有提供额外的意义。谢谢你编辑这个问题,我的语法不好你能有('add','c',2)
?这就是我想要的。。。根据评论中的澄清,似乎最明智的做法是进行产品、组合、连锁。。。不错的组合:)这就是我想要的。。。根据评论中的澄清,似乎最明智的做法是进行产品、组合、连锁。。。不错的组合:)