Created
October 4, 2022 06:33
-
-
Save mafshin/9ee8f2d008fb7b03d1ecdb80ddee2bfa to your computer and use it in GitHub Desktop.
Python All Combinations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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