简体   繁体   中英

Permutations of list of list of lists

I want to find the permutation of list of list of lists For example: my input

x = [[[1,2,3],[5,6,7]],[[8,9,10],[11,12]]]

Required output should be:

[[[1,2,3],[8,9,10]],[[1,2,3],[11,12]],[[5,6,7],[8,9,10]],[[5,6,7],[11,12]]]

As you can see I want the innermost list to be intact and need to have that considered as an element and then do combinations.

I tried permutations(array) in itertools. But it didn't work.

Any help is highly appreciated.

Thank you.

You need itertools.product

import itertools

x = [[[1,2,3],[5,6,7]],[[8,9,10],[11,12]]]
for combo in itertools.product(*x):
    print combo

Output:

([1, 2, 3], [8, 9, 10])
([1, 2, 3], [11, 12])
([5, 6, 7], [8, 9, 10])
([5, 6, 7], [11, 12])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM