How To Generate Lists From A Specification Of Element Combinations
I want to generate a bunch of lists using combinations of elements specified in a form like the following: [[10, 20], [30, 40], [50, 60]] This means that the values available for
Solution 1:
You can create a double-for-loop list comprehension based on the solution you found:
>>> elements = [[10, 20], [30, 40], [50, 60]]
>>> [x for i in range(len(elements)) for x in itertools.product(*elements[:i+1])]
[(10,),
(20,),
(10, 30),
(10, 40),
(20, 30),
(20, 40),
(10, 30, 50),
(10, 30, 60),
(10, 40, 50),
(10, 40, 60),
(20, 30, 50),
(20, 30, 60),
(20, 40, 50),
(20, 40, 60)]
Or maybe a bit cleaner, using enumerate
:
>>>[x for i, _ inenumerate(elements) for x in itertools.product(*elements[:i+1])]
Post a Comment for "How To Generate Lists From A Specification Of Element Combinations"