itertools
основано на генераторе. Редко нужно собирать результаты в списки. Просто создайте свой собственный генератор:
import itertools
def subset_pairs(list_a,list_b):
"""iterator over all pairs of subsets, (s,t), with s drawn from list_a and t drawn from list_b"""
for i in range(1+len(list_a)):
for j in range(1+len(list_b)):
for s in itertools.combinations(list_a,i):
for t in itertools.combinations(list_b,j):
yield s,t
Вот простой тест (с print
в качестве замены для вашей обработки):
for s,t in subset_pairs(['a','b','c'],[1,2]):
print(s,"and",t)
Вывод:
() and ()
() and (1,)
() and (2,)
() and (1, 2)
('a',) and ()
('b',) and ()
('c',) and ()
('a',) and (1,)
('a',) and (2,)
('b',) and (1,)
('b',) and (2,)
('c',) and (1,)
('c',) and (2,)
('a',) and (1, 2)
('b',) and (1, 2)
('c',) and (1, 2)
('a', 'b') and ()
('a', 'c') and ()
('b', 'c') and ()
('a', 'b') and (1,)
('a', 'b') and (2,)
('a', 'c') and (1,)
('a', 'c') and (2,)
('b', 'c') and (1,)
('b', 'c') and (2,)
('a', 'b') and (1, 2)
('a', 'c') and (1, 2)
('b', 'c') and (1, 2)
('a', 'b', 'c') and ()
('a', 'b', 'c') and (1,)
('a', 'b', 'c') and (2,)
('a', 'b', 'c') and (1, 2)