Skip to content

Instantly share code, notes, and snippets.

@mafshin
Created October 4, 2022 06:33
Show Gist options
  • Select an option

  • Save mafshin/9ee8f2d008fb7b03d1ecdb80ddee2bfa to your computer and use it in GitHub Desktop.

Select an option

Save mafshin/9ee8f2d008fb7b03d1ecdb80ddee2bfa to your computer and use it in GitHub Desktop.
Python All Combinations
from functools import reduce
list3 = [
['S1', 'S2'],
['O1', 'O2'],
['F1'],
['A1', 'A2']
]
result = list3[0]
def merge(x, y):
if not isinstance(x, list) and not isinstance(y, list):
return [x,y]
elif isinstance(x, list) and isinstance(y, list):
return x + y
elif isinstance(x, list) and not isinstance(y, list):
x.append(y)
return x
elif not isinstance(x, list) and isinstance(y, list):
y.append(x)
return y
for index in range(1, len(list3)):
next = list3[index]
result = list(map(lambda x: list(map(lambda y: merge(x,y), result)), next))
result = reduce(lambda x, y: x + y, result)
for x in result:
print(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment