[英]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 我想以这样的方式生成排列
The total number of rows remain the same as in the original parent list ie,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']
。
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.