简体   繁体   中英

Python permutation of list of lists of lists

I have a list of lists of lists, such as:

[ 
  [ 
    [ 1, 2, 3 ], [ 3, 2, 1 ], [ 3, 1, 2 ] 
  ], 
  [ 
    [ 4, 5, 6 ], [ 6, 4, 5 ] 
  ],
  [ 
    [ 8, 9, 10 ], [ 10, 8, 9 ], [ 10, 9, 8 ]
  ]
]

I need to find all the permutations of those middle lists, vertically as columns...

[
  [
    [ 1, 4, 8 ], [ 1, 4, 10 ], [ 1, 4, 10 ], 
    [ 1, 6, 8 ], [ 1, 6, 10 ], [ 1, 6, 10 ],
    [ 3, 4, 8 ], [ 3, 4, 10 ], [ 3, 4, 10 ],
    [ 3, 6, 8 ], [ 3, 6, 10 ], [ 3, 6, 10 ],
    [ 3, 4, 8 ], [ 3, 4, 10 ], [ 3, 4, 10 ],
    [ 3, 6, 8 ], [ 3, 6, 10 ], [ 3, 6, 10 ]
  ],
  ...
]

It's hard to explain, but basically, each of those lists with 3 different numbers are all possible "rows" in each row... So row 0 could be 1,2,3 or 3,2,1 or 3,1,2...

What I need to do is find all of the possible columns for each column, which means cycling through each of the possible combinations of the rows and generating the columns from that combination of rows.

If anyone knows what I mean and can word it better, or can help me solve it, please do! I feel like itertools.permutations will help me, but I can't figure out how to tell it to pick 1 of each possible row...

Thanks

So without writing the whole solution out for you, I think you could it fairly easy, you'd just need to write the algorithm that'd crawl the lists in the order you want. Let me see if I can write up a little example:

first loop through:

ixx xxx xxx
ixx xxx xxx
ixx xxx xxx

second loop through:

ixx xxx xxx
ixx xxx xxx
xix xxx xxx

third loop through:

ixx xxx xxx
ixx xxx xxx
xxi xxx xxx

In your biggest list, you could get each individual letter with the y , x , z coords, and just slowly the x and y until they needs to roll over and increment z instead.

                         y  x  z
 first_val = master_list[0][0][0] #1
second_val = master_list[1][0][0] #4
 third_val = master_list[2][0][0] #8

Then you'd have to put the three val s into a similar set of lists the same way.

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