Python 列表列表的所有组合

Python 列表列表的所有组合,python,combinations,Python,Combinations,我基本上是在寻找python版本的 给定一个列表,我需要一个新的列表,它给出列表之间所有可能的项目组合 [[1,2,3],[4,5,6],[7,8,9,10]] -> [[1,4,7],[1,4,8],...,[3,6,10]] 列表的数量是未知的,所以我需要一些适用于所有情况的东西。优雅的奖励积分 您需要: 最优雅的解决方案是在Python2.6中使用 如果您没有使用Python 2.6,那么itertools.product的文档实际上会显示一个等效函数,以“手动”方式完成产品: 我

我基本上是在寻找python版本的

给定一个列表,我需要一个新的列表,它给出列表之间所有可能的项目组合

[[1,2,3],[4,5,6],[7,8,9,10]] -> [[1,4,7],[1,4,8],...,[3,6,10]]
列表的数量是未知的,所以我需要一些适用于所有情况的东西。优雅的奖励积分

您需要:


最优雅的解决方案是在Python2.6中使用

如果您没有使用Python 2.6,那么itertools.product的文档实际上会显示一个等效函数,以“手动”方式完成产品:

我希望你能像我第一次遇到它时一样优雅。

Numpy可以做到:

 >>> import numpy
 >>> a = [[1,2,3],[4,5,6],[7,8,9,10]]
 >>> [list(x) for x in numpy.array(numpy.meshgrid(*a)).T.reshape(-1,len(a))]
[[ 1, 4, 7], [1, 5, 7], [1, 6, 7], ....]

此任务的直接递归没有问题,如果您需要使用字符串的版本,这可能适合您的需要:

combinations = []

def combine(terms, accum):
    last = (len(terms) == 1)
    n = len(terms[0])
    for i in range(n):
        item = accum + terms[0][i]
        if last:
            combinations.append(item)
        else:
            combine(terms[1:], item)


>>> a = [['ab','cd','ef'],['12','34','56']]
>>> combine(a, '')
>>> print(combinations)
['ab12', 'ab34', 'ab56', 'cd12', 'cd34', 'cd56', 'ef12', 'ef34', 'ef56']

可以使用基本python实现这一点。代码需要一个函数来展平列表列表:

def flatten(B):    # function needed for code below;
    A = []
    for i in B:
        if type(i) == list: A.extend(i)
        else: A.append(i)
    return A
然后可以运行:

L = [[1,2,3],[4,5,6],[7,8,9,10]]

outlist =[]; templist =[[]]
for sublist in L:
    outlist = templist; templist = [[]]
    for sitem in sublist:
        for oitem in outlist:
            newitem = [oitem]
            if newitem == [[]]: newitem = [sitem]
            else: newitem = [newitem[0], sitem]
            templist.append(flatten(newitem))

outlist = list(filter(lambda x: len(x)==len(L), templist))  # remove some partial lists that also creep in;
print(outlist)
输出:

[[1, 4, 7], [2, 4, 7], [3, 4, 7], 
[1, 5, 7], [2, 5, 7], [3, 5, 7], 
[1, 6, 7], [2, 6, 7], [3, 6, 7], 
[1, 4, 8], [2, 4, 8], [3, 4, 8], 
[1, 5, 8], [2, 5, 8], [3, 5, 8], 
[1, 6, 8], [2, 6, 8], [3, 6, 8], 
[1, 4, 9], [2, 4, 9], [3, 4, 9], 
[1, 5, 9], [2, 5, 9], [3, 5, 9], 
[1, 6, 9], [2, 6, 9], [3, 6, 9], 
[1, 4, 10], [2, 4, 10], [3, 4, 10], 
[1, 5, 10], [2, 5, 10], [3, 5, 10], 
[1, 6, 10], [2, 6, 10], [3, 6, 10]]
来自itertools导入产品的

list_VAL=[['品牌首字母缩略词:CBIQ','品牌首字母缩略词:KMEFIC',['品牌国家:DXB','品牌国家:BH']]
列表(产品(*列表)
输出:

[[1, 4, 7], [2, 4, 7], [3, 4, 7], 
[1, 5, 7], [2, 5, 7], [3, 5, 7], 
[1, 6, 7], [2, 6, 7], [3, 6, 7], 
[1, 4, 8], [2, 4, 8], [3, 4, 8], 
[1, 5, 8], [2, 5, 8], [3, 5, 8], 
[1, 6, 8], [2, 6, 8], [3, 6, 8], 
[1, 4, 9], [2, 4, 9], [3, 4, 9], 
[1, 5, 9], [2, 5, 9], [3, 5, 9], 
[1, 6, 9], [2, 6, 9], [3, 6, 9], 
[1, 4, 10], [2, 4, 10], [3, 4, 10], 
[1, 5, 10], [2, 5, 10], [3, 5, 10], 
[1, 6, 10], [2, 6, 10], [3, 6, 10]]
[(‘品牌首字母缩略词:CBIQ’,‘品牌国家:DXB’,
(‘品牌首字母缩略词:CBIQ’,‘品牌国家:BH’,
(‘品牌首字母缩略词:KMEFIC’,‘品牌国家:DXB’,
(‘品牌首字母缩略词:KMEFIC’,‘品牌国家:BH’)]


这主要是模仿解决方案,如使用,但有以下区别:

  • 这会将参数以内联方式传递给
    itertools.product
    ,而不是通过变量
    a
    -因此内联参数不需要
    *args
    语法
  • 如果您的行为与我的类似,并且您可以让您的代码以其他方式使用带有内联
    product
    参数的
    *args
    语法(如
    product(*[[1,2,3],[4,5,6],[7,8,9,10])
    mypy
    可能仍然会失败(类似
    错误:没有重载变量“product”匹配参数类型”列表)[对象]“
  • 因此,解决该问题的方法是不使用
    *args
    语法,如下所示:

这个分号是怎么回事?:)习惯的力量。我喜欢Python让你放一个分号,只是为了帮助我们这些C/Java程序员。但很明显,当你做一些像print(“foo”);的事情时,它并不是一个真正的语句终止符,这在C或Java中是完全合法的(尽管毫无意义)但是在Python中被禁止。有人能解释一下
*a
中星号的含义吗?
*a
意味着这些是传递给函数或方法的参数。
def fn(a,b,c):
将响应
fn(*[1,2,3])
@mjallday,是否可以添加这些组合:(7,4,1),(8,4,1),(9,4,1),(10,4,1),(7,5,1),(8,5,1),(9,5,1),(10,5,1)等等?@Reman不完全清楚您想要得到什么,但如果它也是每个元组的倒数,您可以使用一个包装器函数,它以
a
作为输入,在
itertools.product(*a)上迭代
产生
itertools产生的元组和反向版本(例如,创建一个列表,
reverse()
并将其转换回元组)。最好问一个新问题。如果你熟悉javascript
[*a]
将是相同的js spread操作符
[…a]
。这对dicts也是如此。有人能解释一下吗?如果a中的东西是数组,则这不起作用。
a=[[array a]、[array B]、[array C]]、[…]
这感觉就像是在问“我怎样烧开水”,而不是说“使用水壶”,你得到的是“降低水周围的大气压力”。这两个答案都有效!这个答案应该被接受,因为它是唯一一个使用内置功能的答案,同时强调它也适用于任何类型和异构类型。这个答案有什么不同与多年前提供的参数相比?这里的参数类型是同质的,而不是异质的。
L = [[1,2,3],[4,5,6],[7,8,9,10]]

outlist =[]; templist =[[]]
for sublist in L:
    outlist = templist; templist = [[]]
    for sitem in sublist:
        for oitem in outlist:
            newitem = [oitem]
            if newitem == [[]]: newitem = [sitem]
            else: newitem = [newitem[0], sitem]
            templist.append(flatten(newitem))

outlist = list(filter(lambda x: len(x)==len(L), templist))  # remove some partial lists that also creep in;
print(outlist)
[[1, 4, 7], [2, 4, 7], [3, 4, 7], 
[1, 5, 7], [2, 5, 7], [3, 5, 7], 
[1, 6, 7], [2, 6, 7], [3, 6, 7], 
[1, 4, 8], [2, 4, 8], [3, 4, 8], 
[1, 5, 8], [2, 5, 8], [3, 5, 8], 
[1, 6, 8], [2, 6, 8], [3, 6, 8], 
[1, 4, 9], [2, 4, 9], [3, 4, 9], 
[1, 5, 9], [2, 5, 9], [3, 5, 9], 
[1, 6, 9], [2, 6, 9], [3, 6, 9], 
[1, 4, 10], [2, 4, 10], [3, 4, 10], 
[1, 5, 10], [2, 5, 10], [3, 5, 10], 
[1, 6, 10], [2, 6, 10], [3, 6, 10]]
    >>> import itertools
    >>> list(itertools.product([1,2,3],[4,5,6],[7,8,9,10]))
    [(1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 4, 10), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 5, 10), (1, 6, 7), (1, 6, 8), (1, 6, 9), (1, 6, 10), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 4, 10), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 5, 10), (2, 6, 7), (2, 6, 8), (2, 6, 9), (2, 6, 10), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 4, 10), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 5, 10), (3, 6, 7), (3, 6, 8), (3, 6, 9), (3, 6, 10)]