Используйте комбинацию itertools
функций combinations
, product
и chain
:
list1 = ["a", "b", "c", "d"]
list2 = [1, 2, 3]
import itertools
comb1 = itertools.combinations(list1, 3)
comb2 = itertools.combinations(list2, 2)
result = itertools.product(comb1, comb2)
result = [list(itertools.chain.from_iterable(x)) for x in result]
Результат:
[['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]]
Здесь у вас есть живой пример