简体   繁体   English

列表列表的Python排列

[英]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... 很难解释,但是基本上,每个具有3个不同数字的列表在每一行中都是可能的“行” ...因此,行0可以是1,2,3或3,2,1或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... 我觉得itertools.permutations将对我有帮助,但是我不知道如何告诉它从每个可能的行中选择1个。

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. 在最大的列表中,您可以得到每个带有yxz坐标的字母,然后慢慢地将xy调到它们需要翻转并递增z为止。

                         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. 然后,您必须以相同的方式val三个val放入一组相似的列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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