I want to find a combination using itertools in a three-dimensional array.

Asked 2 years ago, Updated 2 years ago, 16 views

I'm thinking of finding a combination using itertools in a three-dimensional array.
I would like to find the total combination of li1[i] to choose one by one.

 li1 = [[0,2], [0,1,2]], 
     [[3]], 
     [[7, 9], [7,8,9],[7,6,9]], 
     [[11]], 
     [[12, 10, 20], [12, 14, 18, 20]]]

For example, if you combine the beginnings of li1[i], li1[0]→li1[1][0]→li1[2][0]→li1[3][0]li1[4][0] to [0,2,3,7,9,11,12,10,20].

result=[0, 2, 3, 7, 9, 11, 12, 10, 20], [0, 2, 3, 7, 9, 11, 14, 18, 20],
        [0,2,3,7, 8, 9,11,12,10,20],[0,2,3,7, 8, 9,11,12,14,18,20],
        [0,2,3,7,6,9,11,12,10,20],[0,2,3,7,6,9,11,12,14,18,20],
        [0,1,2,3,7,9,11,12,10,20],[0,1,2,3,7,9,11,12,14,18,20],
        [0,1,2,3,7,8,9,11,12,10,20],[0,1,2,3,7,8,9,11,12,14,18,20],
        [0,1,2,3,7,6,9,11,12,10,20],[0,1,2,3,7,6,9,11,12,14,18,20]]

It doesn't matter if you don't use itertools.Thank you for your cooperation.

python

2022-09-30 13:48

1 Answers

>>import intertools
>> [sum(list(l), []) for lin apply (itertools.product, li1)]


2022-09-30 13:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.