Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 不同尺寸的笛卡尔积_Python_List_Itertools_Cartesian Product - Fatal编程技术网

Python 不同尺寸的笛卡尔积

Python 不同尺寸的笛卡尔积,python,list,itertools,cartesian-product,Python,List,Itertools,Cartesian Product,由于使用了itertools.product()函数,我可以得到列表的笛卡尔乘积: lists = [['A', 'B'], ['1', '2'], ['x', 'y']] combinations = itertools.product(*lists) # [('A', '1', 'x'), ('A', '2', 'y'), ..., ('B', '2', 'y')] 我想要的是相同的东西,但尺寸不同: all_comb = magicfunction(lists) # [('A', '1'

由于使用了
itertools.product()
函数,我可以得到列表的笛卡尔乘积:

lists = [['A', 'B'], ['1', '2'], ['x', 'y']]
combinations = itertools.product(*lists)
# [('A', '1', 'x'), ('A', '2', 'y'), ..., ('B', '2', 'y')]
我想要的是相同的东西,但尺寸不同:

all_comb = magicfunction(lists)
# [('A', '1', 'x'), ..., ('B', '2', 'y'), ('A', '1'), ('A', '2'), ... ('2', 'y'), ... ('y')]
我看不出一个明显的方法

我需要一种方法,可以让我设置元组的最小和最大大小(我处理长列表,只需要7到3个大小的组合,列表的数量和大小各不相同)

我的列表更像:

lists = [['A', 'B', 'C'], ['1', '2'], ['x', 'y', 'z', 'u'], ...] # size may go to a few dozens

只需根据较小尺寸的组合将多个产品连接在一起:

from itertools import chain, product, combinations

def ranged_product(*lists, **start_stop):
    start, stop = start_stop.get('start', len(lists)), start_stop.get('stop', 0)
    return chain.from_iterable(product(*comb)
                               for size in xrange(start, stop - 1, -1)
                               for comb in combinations(lists, r=size))
演示:


我误解了combinaisons()的第二个参数的用途,因为文档在示例中使用字符串作为iterables。有一个明显的方法可以做到这一点。谢谢。我想我可以通过限制范围来设置最大大小。虽然我发现您处理开始/停止的方式有些奇怪,但您的回答基本上得出了相同的结论:“使用组合的第二个参数”。您的答案提供了一个函数,允许轻松设置最小值和最大值。但是,我不需要根据我的数据设置不同的最小值/最大值,只需在调用范围中使用最小值(maxsize,len(lists)),即可解决此问题。谢谢您的回答和演示:)@sildar:start和stop处理取决于
range()
如何处理这个问题,但在这种情况下可能会有点过于复杂。我会简化的。
from itertools import chain, product, combinations

def ranged_product(*lists, **start_stop):
    start, stop = start_stop.get('start', len(lists)), start_stop.get('stop', 0)
    return chain.from_iterable(product(*comb)
                               for size in xrange(start, stop - 1, -1)
                               for comb in combinations(lists, r=size))
>>> lists = [['A', 'B'], ['1', '2'], ['x', 'y']]
>>> for prod in ranged_product(stop=2, *lists):
...     print prod
... 
('A', '1', 'x')
('A', '1', 'y')
('A', '2', 'x')
('A', '2', 'y')
('B', '1', 'x')
('B', '1', 'y')
('B', '2', 'x')
('B', '2', 'y')
('A', '1')
('A', '2')
('B', '1')
('B', '2')
('A', 'x')
('A', 'y')
('B', 'x')
('B', 'y')
('1', 'x')
('1', 'y')
('2', 'x')
('2', 'y')