如何将iterable1中的n个项目与来自iterable2的m个项目组合在一起?
即
iterable1 = [0,1,2,3,4]
iterable2 = ['a','b','c']
BlackBox(itertools.combination(iterable1, 2),itertools.combination(iterable2, 1)) yields
(0,1,'a'), (0,1,'b'), (0,1,'c'), (0,2,'a'), (0,3,'a'), etc. Order doesn't matter
我收到一个元素列表,其中可能包含一个通配符,然后我需要用所有通配符的可能值替换它。我检查通配符的数量,并需要将这么多元素的组合添加到我的去通配符列表中。换句话说,iterable2是通配符的所有可能值,m是通配符的数量,iterable1是删除了所有通配符的原始列表,n是所需项目的数量减去m。
答案 0 :(得分:1)
>>> iterable1 = [0,1,2,3,4]
>>> iterable2 = ['a','b','c']
>>> import itertools as it
>>> list(x+y for x,y in it.product(it.combinations(iterable1, 2), it.combinations(iterable2, 1)))
[(0, 1, 'a'), (0, 1, 'b'), (0, 1, 'c'), (0, 2, 'a'), (0, 2, 'b'), (0, 2, 'c'), (0, 3, 'a'), (0, 3, 'b'), (0, 3, 'c'), (0, 4, 'a'), (0, 4, 'b'), (0, 4, 'c'), (1, 2, 'a'), (1, 2, 'b'), (1, 2, 'c'), (1, 3, 'a'), (1, 3, 'b'), (1, 3, 'c'), (1, 4, 'a'), (1, 4, 'b'), (1, 4, 'c'), (2, 3, 'a'), (2, 3, 'b'), (2, 3, 'c'), (2, 4, 'a'), (2, 4, 'b'), (2, 4, 'c'), (3, 4, 'a'), (3, 4, 'b'), (3, 4, 'c')]