python - 生成二维数组的所有唯一排列

标签 python

我希望能够在 python 中生成二维数组的所有唯一排列。

例如拿这个二维数组 [[1,1],[0,0]] 我想回来

[[0,0],
[1,1]]

[[0,1],
[0,1]]

[[0,1]
[1,0]]

[[1,0]
[0,1]]

[[1,0]
[1,0]]

[[1,1]
[0,0]]

最佳答案

你可以这样做

d = [[1, 1], [0, 0]]
from itertools import permutations, chain
from pprint import pprint
pprint(sorted([i[:2], i[2:]] for i in set(permutations(chain.from_iterable(d)))))

输出

[[[0, 0], [1, 1]],
 [[0, 1], [0, 1]],
 [[0, 1], [1, 0]],
 [[1, 0], [0, 1]],
 [[1, 0], [1, 0]],
 [[1, 1], [0, 0]]]

关于python - 生成二维数组的所有唯一排列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21959530/

相关文章:

python - 无法从 TensorFlow 或 Keras 中的 Google Cloud Storage 存储桶加载图像

python - 如何在 Windows 上使用 Python 读取系统信息?

python - 正则表达式处理后在句末添加标点符号: Python

python - 初始化字体需要很长时间

python - 使用 SQLAlchemy bulk_insert_mappings() 时批处理我的插入是否更快?

python - Django 和单位转换

python - 如何限制用户可以上传到我的表单的文件类型?

python - 如何在具有多对多关系的SQLAlchemy表中插入数据?

python - Bellman-Ford算法的python实现

python - sklearn MinMaxScaler() 与 groupby pandas