itertools - не лучший способ справиться с вашей заботой.
Если у вас есть только 3 комплекта для объединения, просто зациклитесь, а когда вы потерпите неудачу, разорвите петли. (Если код сложный, установите переменную и прервитесь прямо на улице.
for i1 in [A, B]:
for i2 in [C, D]:
for i3 in [E, F, G]:
if not test(i1, i2, i3):
break
Однако, если количество ваших наборов является переменным, используйте рекурсивную функцию (возврат):
inp_sets = ([A,B],[C,D],[E,F,G])
max_col = len(inp_sets)
def generate(col_index, current_set):
if col_index == max_col:
if test(current_set):
return current_set
else:
return None
else:
found = False
for item in inp_sets[col_index]:
res = generate(col_index+1, current_set + [item]):
if res:
return res
elif (col_index == max_col - 1):
# Here we are skipping the rest of the checks for last column
# Change the condition if you want to skip for more columns
return None
result = generate(0, [])