简体   繁体   English

生成python列表的二维排列

[英]Generate 2 dimensional permutations of a python list

I have a python list as follows: 我有一个Python列表如下:

[['row_0','row_0_0','row_0_1'],['row_1','row_1_0','row_1_1']]

where each list in the parent list is called a row and each item in child list is called a column . 其中父列表中的每个列表称为row ,子列表中的每个项目称为column For example: ['row_0','row_0_0','row_0_1'] is a row. 例如: ['row_0','row_0_0','row_0_1']是一行。 and 'row_0','row_0_0' and 'row_0_1' are columns of this row. “ row_0”,“ row_0_0”和“ row_0_1”是该行的列。

I want to generate permutations in such a way that 我想以这样的方式生成排列

  1. The total number of rows remain the same as in the original parent list ie,2. 行的总数与原始父列表中的相同,即2。

  2. The permutations of the columns of each row remain within that row. 每行列的排列保留在该行内。 For example: ['row_0','row_0_0','row_0_1'] can have a permutation ['row_0','row_0_1','row_0_0'] , ['row_0','row_0_0','row_0_1'] . 例如: ['row_0','row_0_0','row_0_1']可以有一个排列['row_0','row_0_1','row_0_0']['row_0','row_0_0','row_0_1']

  3. The first column of each row never changes in permutations. 每行的第一列从不更改排列。 For example: 'row_0' in and 'row_1' always remain the first items in their list. 例如:“ row_0”和“ row_1”始终保持列表中的第一项。

So far what I have implemented looks like this: 到目前为止,我已经实现了以下内容:

perm_list =  [['row_0','row_0_0','row_0_1'],['row_1','row_1_0','row_1_1']]
all_permutations = list(itertools.permutations(perm_list, len(perm_list))
print(all_permutations)

but this only generates permutations at the parent list level. 但这只会在父级列表级别生成排列。 I was wondering if python has a builtin tool to handle a functionality like this that can be tweaked to fit my needs. 我想知道python是否具有内置工具来处理这样的功能,可以对其进行调整以满足我的需求。 Any suggestions will be appreciated. 任何建议将不胜感激。

EDIT 编辑

The output I am looking for is something like this: 我正在寻找的输出是这样的:

[
  [['row_0','row_0_0','row_0_1'],['row_1','row_1_0','row_1_1']],
  [['row_0','row_0_1','row_0_0'],['row_1','row_1_1','row_1_0']],
  [['row_1','row_1_0','row_1_1'],['row_0','row_0_0','row_0_1']],
  [['row_1','row_1_1','row_1_0'],['row_0','row_0_1','row_0_0']],
]

a. 一种。 The first item in each child list remained the same. 每个子列表中的第一项保持不变。 b. The items in each child list remained within that list. 每个子列表中的项目都保留在该列表中。 c. C。 The total number of items for both parent and child list remained the same. 父级和子级列表的项目总数保持不变。

From what I know , there is no build-in function for what you described 据我了解,您所描述的没有内置功能

l1=[[list((x[0],)+y) for y in itertools.permutations(x[1:], len(x)-1)] for x in l ]
list(map(list,itertools.product(l1[0],l1[1])))
[[['row_0', 'row_0_0', 'row_0_1'], ['row_1', 'row_1_0', 'row_1_1']], 
[['row_0', 'row_0_0', 'row_0_1'], ['row_1', 'row_1_1', 'row_1_0']], 
[['row_0', 'row_0_1', 'row_0_0'], ['row_1', 'row_1_0', 'row_1_1']], 
[['row_0', 'row_0_1', 'row_0_0'], ['row_1', 'row_1_1', 'row_1_0']]]

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

相关问题 在Python中生成列表的所有排列时,列表中元素的最大数量 - The maximum number of element in a list when generate all the permutations of a list in Python 在python中生成具有6维唯一元素的列表 - generate a list with 6-dimensional unique elements in python 如何在python中生成多维列表 - How to generate multi dimensional list in python Python-如何生成大小大于列表元素个数的排列 - Python - How to generate permutations of size greater than the number of list elements 如何使用生成器在 Python 中生成没有“反向重复”的列表的排列 - How to generate permutations of a list without “reverse duplicates” in Python using generators 递归算法,用于在Python中生成列表长度k的所有排列 - Recursive Algorithm to generate all permutations of length k of a list in Python 如何在不“移动”零的情况下生成列表的排列。 在Python中 - How to generate permutations of a list without “moving” zeros. in Python 如何在没有itertools.combinations的情况下在Python中生成列表的递增排列 - How to generate increasing permutations of a list in Python without itertools.combinations 如何在 Python 中的列表中为多个分组生成排列 - How can I generate Permutations for multiple groupings in a list in Python Python - 从键值对列表中按键生成排列 - Python - Generate permutations by key from key value pair list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM