简体   繁体   中英

How to get all combinations of 2 lists?

How do I do this in Python?

Input:

num = [1, 2, 3]
alpha = ['a', 'b', 'c']

Output:

[1, a] [1, a, b] [1, a, b, c] [1, b, c] [1, c]
[1, 2, a] [1, 2, a, b] ... [1, 2, c]
...
...
[3, a] [3, a, b] ... [3, c]

Constraints:

The output expect all possible consecutive combinations
e.g. ['a'] ['a','b'] are desired while ['a','c'] is not.

I tried a nested of 4 for-loops/4 while-loops. Is it common to use such depth of loops?

You can use module itertools ans combinations

import itertools as it
l1 = []
# get all combinations of num
for i in range(1, len(num)+1):
    l1.extend([* it.combinations(num, i)])

# get all combinations of alpha
for j in range(1, len(alpha)+1):
    l2.extend([* it.combinations(alpha, j)])

# list comprehension to combine elements from the two lists
comb = [e1+e2 for e1 in l1 for e2 in l2]       

By doing this, you get a list of tuples.

Update:

In order to take into account the constraint:

The output expect all possible consecutive combinations

eg [a] [a,b] are desired while [a,c] is not.

num = [1, 2, 3]
alpha = ['a', 'b', 'c']
l1 = [num[i:j+1] for i in range(len(num)) for j in range(i, len(num))]
l2 = [alpha[i:j+1] for i in range(len(alpha)) for j in range(i, len(alpha))]

result = [e1+e2 for e1 in l1 for e2 in l2]
print(*result, sep="\n")
[1, 'a']
[1, 'a', 'b']
[1, 'a', 'b', 'c']
[1, 'b']
[1, 'b', 'c']
[1, 'c']
[1, 2, 'a']
[1, 2, 'a', 'b']
[1, 2, 'a', 'b', 'c']
[1, 2, 'b']
[1, 2, 'b', 'c']
[1, 2, 'c']
[1, 2, 3, 'a']
[1, 2, 3, 'a', 'b']
[1, 2, 3, 'a', 'b', 'c']
[1, 2, 3, 'b']
[1, 2, 3, 'b', 'c']
[1, 2, 3, 'c']
[2, 'a']
[2, 'a', 'b']
[2, 'a', 'b', 'c']
[2, 'b']
[2, 'b', 'c']
[2, 'c']
[2, 3, 'a']
[2, 3, 'a', 'b']
[2, 3, 'a', 'b', 'c']
[2, 3, 'b']
[2, 3, 'b', 'c']
[2, 3, 'c']
[3, 'a']
[3, 'a', 'b']
[3, 'a', 'b', 'c']
[3, 'b']
[3, 'b', 'c']
[3, 'c']

Following uses only two nested for loops

from itertools import product as prod

def consecutive_combos(a):
    " Generates consecutive ombinations of items in list "
    return [a[i:j] for i in range(len(a)) for j in range(i+1, len(a)+1)]

num = [1, 2, 3]
alpha = ['a', 'b', 'c']

# Generates product of sequences
result = [x + y for x, y in prod(consecutive_combos(num), consecutive_combos(alpha))]

print(result)

Output

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

i hope it will help

from itertools import combinations 
num = [1, 2, 3]
alpha = ["a", "b", "c"]
big_list=num+alpha
comb += list(combinations(big_list,2) )
comb += list(combinations(big_list,3) )
for i in list(comb): 
   print (i)

output:

(1, 2) (1, 3) (1, 'a') (1, 'b') (1, 'c') (2, 3) (2, 'a') (2, 'b') (2, 'c') (3, 'a') (3, 'b') (3, 'c') ('a', 'b') ('a', 'c') ('b', 'c') (1, 2, 3) (1, 2, 'a') (1, 2, 'b') (1, 2, 'c') (1, 3, 'a') (1, 3, 'b') (1, 3, 'c') (1, 'a', 'b') (1, 'a', 'c') (1, 'b', 'c') (2, 3, 'a') (2, 3, 'b') (2, 3, 'c') (2, 'a', 'b') (2, 'a', 'c') (2, 'b', 'c') (3, 'a', 'b') (3, 'a', 'c') (3, 'b', 'c') ('a', 'b', 'c') [Finished in 0.50s]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM