Python 两个列表中元素的组合

Python 两个列表中元素的组合,python,list,combinatorics,Python,List,Combinatorics,我有两份清单: list1 = ["a", "b", "c", "d"] list2 = [1, 2, 3] 我想从列表1中选取3个元素,从列表2中选取2个元素,进行如下组合,总共12个组合: [a b c 1 2] [a b c 1 3] [a b c 2 3] [a b d 1 2] [a b d 1 3] [a b d 2 3] [a c d 1 2] [a c d 1 3] [a c d 2 3] [b c d 1 2] [b c d 1 3] [b c d 2 3] 这是我的代码,

我有两份清单:

list1 = ["a", "b", "c", "d"]
list2 = [1, 2, 3]
我想从列表1中选取3个元素,从列表2中选取2个元素,进行如下组合,总共12个组合:

[a b c 1 2]
[a b c 1 3]
[a b c 2 3]
[a b d 1 2]
[a b d 1 3]
[a b d 2 3]
[a c d 1 2]
[a c d 1 3]
[a c d 2 3]
[b c d 1 2]
[b c d 1 3]
[b c d 2 3]
这是我的代码,但它不起作用:

import itertools
from itertools import combinations 

def combi(arr, r): 
    return list(combinations(arr, r)) 

# Driver Function 
if __name__ == "__main__": 
    a = ["a", "b", "c", "d"] 
    r = 3
    a= combi(arr, r)
    print (a)
    b = [1, 2, 3]
    s =2
    b = combi(brr, s)
    print (b)
    crr = a + b
    print (crr)
    c = combi(crr, 2)
    print (c)
    for i in range(len(c)):
        for j in range(len(c)):
            print c[i][j]
            print '\n' 
使用功能组合、产品和链的组合:

结果:

[['a', 'b', 'c', 1, 2],
 ['a', 'b', 'c', 1, 3],
 ['a', 'b', 'c', 2, 3],
 ['a', 'b', 'd', 1, 2],
 ['a', 'b', 'd', 1, 3],
 ['a', 'b', 'd', 2, 3],
 ['a', 'c', 'd', 1, 2],
 ['a', 'c', 'd', 1, 3],
 ['a', 'c', 'd', 2, 3],
 ['b', 'c', 'd', 1, 2],
 ['b', 'c', 'd', 1, 3],
 ['b', 'c', 'd', 2, 3]]
这里您可以使用功能组合、产品和链的组合:

结果:

[['a', 'b', 'c', 1, 2],
 ['a', 'b', 'c', 1, 3],
 ['a', 'b', 'c', 2, 3],
 ['a', 'b', 'd', 1, 2],
 ['a', 'b', 'd', 1, 3],
 ['a', 'b', 'd', 2, 3],
 ['a', 'c', 'd', 1, 2],
 ['a', 'c', 'd', 1, 3],
 ['a', 'c', 'd', 2, 3],
 ['b', 'c', 'd', 1, 2],
 ['b', 'c', 'd', 1, 3],
 ['b', 'c', 'd', 2, 3]]

这里有一个

这里有一个可能适合您的方法:

>>> from itertools import combinations
>>> list1 = ["a", "b", "c", "d"]
>>> list2 = [1, 2, 3]
>>> [[*x, *y] for x in combinations(list1, 3) for y in combinations(list2, 2)]
[['a', 'b', 'c', 1, 2], ['a', 'b', 'c', 1, 3], ['a', 'b', 'c', 2, 3], ['a', 'b', 'd', 1, 2], ['a', 'b', 'd', 1, 3], ['a', 'b', 'd', 2, 3], ['a', 'c', 'd', 1, 2], ['a', 'c', 'd', 1, 3], ['a', 'c', 'd', 2, 3], ['b', 'c', 'd', 1, 2], ['b', 'c', 'd', 1, 3], ['b', 'c', 'd', 2, 3]]

以下是一种可能适合您的方法:

>>> from itertools import combinations
>>> list1 = ["a", "b", "c", "d"]
>>> list2 = [1, 2, 3]
>>> [[*x, *y] for x in combinations(list1, 3) for y in combinations(list2, 2)]
[['a', 'b', 'c', 1, 2], ['a', 'b', 'c', 1, 3], ['a', 'b', 'c', 2, 3], ['a', 'b', 'd', 1, 2], ['a', 'b', 'd', 1, 3], ['a', 'b', 'd', 2, 3], ['a', 'c', 'd', 1, 2], ['a', 'c', 'd', 1, 3], ['a', 'c', 'd', 2, 3], ['b', 'c', 'd', 1, 2], ['b', 'c', 'd', 1, 3], ['b', 'c', 'd', 2, 3]]

您可以使用嵌套循环。以下是不带任何libraryworks的代码,仅当您从每个列表中保留一个元素时

list3=[]
for a in list1:
    st=''
    for t in list1:
        if(t!=a):
            st=st+t+' '
    for b in list2:
        st1=''
        for m in list2:
            if(m!=b):
                 st1=st1+m+' '
        list3.append(st+st1.strip())

您可以使用嵌套循环。以下是不带任何libraryworks的代码,仅当您从每个列表中保留一个元素时

list3=[]
for a in list1:
    st=''
    for t in list1:
        if(t!=a):
            st=st+t+' '
    for b in list2:
        st1=''
        for m in list2:
            if(m!=b):
                 st1=st1+m+' '
        list3.append(st+st1.strip())