我希望能够在python中生成二维数组的所有唯一排列。
例如,使用这个2d数组 [[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]]
答案 0 :(得分:2)
你可以这样做
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]]]
答案 1 :(得分:1)
这是您阵列的大致尺寸吗?如果它很大,这个解决方案将会非常缓慢,但最终会有效。对于这种大小的数组,python内置的itertools是可行的方法,加上一些numpy操作。
此外,唯一排列的数量取决于初始数组中不同的元素数量。如此扁平化阵列,产生所有排列,重新塑造成2x2(或您想要的大小),并且比较将为您提供“独特”阵列,就像您似乎意味着它。
我在这里使用了循环(而不是理解)来使事情易于阅读/测试/检查。在用于真实之前,绝对可以转化为理解(更快,更好)。
a = np.array([[1,1],[0,0]]).flatten()
permutes = []
for i in permutations(a):
permutes.append((np.array(i).reshape((2,2))))
unique_permutes = [permutes[0]]
for i in permutes[1:]:
one_equal = False
for unique in unique_permutes:
if np.array_equal(i, unique):
one_equal = True
break
if not one_equal:
unique_permutes.append(i)
print len(unique_permutes) #same as what you wanted
for i in unique_permutes: #prints pretilly for sanity checking
print i
答案 2 :(得分:1)
一种 - 不是特别有效 - 这样做的方式就像是
from itertools import permutations, chain, islice
def uniperm_arrays(arr):
flat = chain.from_iterable(arr)
perms = set(permutations(flat))
for perm in perms:
pit = iter(perm)
yield [list(islice(pit, len(row))) for row in arr]
给出了
>>> uu = uniperm_arrays([[1,1],[0,0]])
>>> for u in uu:
... for row in u:
... print(row)
... print()
...
[1, 0]
[1, 0]
[1, 1]
[0, 0]
[0, 0]
[1, 1]
[1, 0]
[0, 1]
[0, 1]
[1, 0]
[0, 1]
[0, 1]
答案 3 :(得分:0)
编辑这应该适用于任何尺寸和形状的2d数组。
基于这样的想法,即排列实际上只是结构为2D列表的平数序列:
from itertools import permutations
def tbl_perms(table):
flat = (j for i in table for j in i)
flat_permutations = iter(sorted(set(permutations(flat))))
# convert back to the original structure
while flat_permutations:
flat_table = list(flat_permutations.next()) # because you can't pop() from tuple
yield [[flat_table.pop(0) for _ in row] for row in table]
result = tbl_perms([[1, 1], [0, 0]])
pprint(list(result))
result = tbl_perms([[1, 1, 1], [0, 0, 0]])
pprint(list(result))
result = tbl_perms([[1, 2, 3], ['a', 'b']])
pprint(list(result))
<强>输出:强>
[[[0, 0], [1, 1]], [[0, 1], [0, 1]], [[0, 1], [1, 0]], [[1, 0], [0, 1]], [[1, 0], [1, 0]], [[1, 1], [0, 0]]]
[[[0, 0, 0], [1, 1, 1]], [[0, 0, 1], [0, 1, 1]], [[0, 0, 1], [1, 0, 1]], [[0, 0, 1], [1, 1, 0]], [[0, 1, 0], [0, 1, 1]], [[0, 1, 0], [1, 0, 1]], [[0, 1, 0], [1, 1, 0]], [[0, 1, 1], [0, 0, 1]], [[0, 1, 1], [0, 1, 0]], [[0, 1, 1], [1, 0, 0]], [[1, 0, 0], [0, 1, 1]], [[1, 0, 0], [1, 0, 1]], [[1, 0, 0], [1, 1, 0]], [[1, 0, 1], [0, 0, 1]], [[1, 0, 1], [0, 1, 0]], [[1, 0, 1], [1, 0, 0]], [[1, 1, 0], [0, 0, 1]], [[1, 1, 0], [0, 1, 0]], [[1, 1, 0], [1, 0, 0]], [[1, 1, 1], [0, 0, 0]]]
[[[1, 2, 3], ['a', 'b']], [[1, 2, 3], ['b', 'a']], [[1, 2, 'a'], [3, 'b']], [[1, 2, 'a'], ['b', 3]], [[1, 2, 'b'], [3, 'a']], [[1, 2, 'b'], ['a', 3]], [[1, 3, 2], ['a', 'b']], [[1, 3, 2], ['b', 'a']], [[1, 3, 'a'], [2, 'b']], [[1, 3, 'a'], ['b', 2]], [[1, 3, 'b'], [2, 'a']], [[1, 3, 'b'], ['a', 2]], [[1, 'a', 2], [3, 'b']], [[1, 'a', 2], ['b', 3]], [[1, 'a', 3], [2, 'b']], [[1, 'a', 3], ['b', 2]], [[1, 'a', 'b'], [2, 3]], [[1, 'a', 'b'], [3, 2]], [[1, 'b', 2], [3, 'a']], [[1, 'b', 2], ['a', 3]], [[1, 'b', 3], [2, 'a']], [[1, 'b', 3], ['a', 2]], [[1, 'b', 'a'], [2, 3]], [[1, 'b', 'a'], [3, 2]], [[2, 1, 3], ['a', 'b']], [[2, 1, 3], ['b', 'a']], [[2, 1, 'a'], [3, 'b']], [[2, 1, 'a'], ['b', 3]], [[2, 1, 'b'], [3, 'a']], [[2, 1, 'b'], ['a', 3]], [[2, 3, 1], ['a', 'b']], [[2, 3, 1], ['b', 'a']], [[2, 3, 'a'], [1, 'b']], [[2, 3, 'a'], ['b', 1]], [[2, 3, 'b'], [1, 'a']], [[2, 3, 'b'], ['a', 1]], [[2, 'a', 1], [3, 'b']], [[2, 'a', 1], ['b', 3]], [[2, 'a', 3], [1, 'b']], [[2, 'a', 3], ['b', 1]], [[2, 'a', 'b'], [1, 3]], [[2, 'a', 'b'], [3, 1]], [[2, 'b', 1], [3, 'a']], [[2, 'b', 1], ['a', 3]], [[2, 'b', 3], [1, 'a']], [[2, 'b', 3], ['a', 1]], [[2, 'b', 'a'], [1, 3]], [[2, 'b', 'a'], [3, 1]], [[3, 1, 2], ['a', 'b']], [[3, 1, 2], ['b', 'a']], [[3, 1, 'a'], [2, 'b']], [[3, 1, 'a'], ['b', 2]], [[3, 1, 'b'], [2, 'a']], [[3, 1, 'b'], ['a', 2]], [[3, 2, 1], ['a', 'b']], [[3, 2, 1], ['b', 'a']], [[3, 2, 'a'], [1, 'b']], [[3, 2, 'a'], ['b', 1]], [[3, 2, 'b'], [1, 'a']], [[3, 2, 'b'], ['a', 1]], [[3, 'a', 1], [2, 'b']], [[3, 'a', 1], ['b', 2]], [[3, 'a', 2], [1, 'b']], [[3, 'a', 2], ['b', 1]], [[3, 'a', 'b'], [1, 2]], [[3, 'a', 'b'], [2, 1]], [[3, 'b', 1], [2, 'a']], [[3, 'b', 1], ['a', 2]], [[3, 'b', 2], [1, 'a']], [[3, 'b', 2], ['a', 1]], [[3, 'b', 'a'], [1, 2]], [[3, 'b', 'a'], [2, 1]], [['a', 1, 2], [3, 'b']], [['a', 1, 2], ['b', 3]], [['a', 1, 3], [2, 'b']], [['a', 1, 3], ['b', 2]], [['a', 1, 'b'], [2, 3]], [['a', 1, 'b'], [3, 2]], [['a', 2, 1], [3, 'b']], [['a', 2, 1], ['b', 3]], [['a', 2, 3], [1, 'b']], [['a', 2, 3], ['b', 1]], [['a', 2, 'b'], [1, 3]], [['a', 2, 'b'], [3, 1]], [['a', 3, 1], [2, 'b']], [['a', 3, 1], ['b', 2]], [['a', 3, 2], [1, 'b']], [['a', 3, 2], ['b', 1]], [['a', 3, 'b'], [1, 2]], [['a', 3, 'b'], [2, 1]], [['a', 'b', 1], [2, 3]], [['a', 'b', 1], [3, 2]], [['a', 'b', 2], [1, 3]], [['a', 'b', 2], [3, 1]], [['a', 'b', 3], [1, 2]], [['a', 'b', 3], [2, 1]], [['b', 1, 2], [3, 'a']], [['b', 1, 2], ['a', 3]], [['b', 1, 3], [2, 'a']], [['b', 1, 3], ['a', 2]], [['b', 1, 'a'], [2, 3]], [['b', 1, 'a'], [3, 2]], [['b', 2, 1], [3, 'a']], [['b', 2, 1], ['a', 3]], [['b', 2, 3], [1, 'a']], [['b', 2, 3], ['a', 1]], [['b', 2, 'a'], [1, 3]], [['b', 2, 'a'], [3, 1]], [['b', 3, 1], [2, 'a']], [['b', 3, 1], ['a', 2]], [['b', 3, 2], [1, 'a']], [['b', 3, 2], ['a', 1]], [['b', 3, 'a'], [1, 2]], [['b', 3, 'a'], [2, 1]], [['b', 'a', 1], [2, 3]], [['b', 'a', 1], [3, 2]], [['b', 'a', 2], [1, 3]], [['b', 'a', 2], [3, 1]], [['b', 'a', 3], [1, 2]], [['b', 'a', 3], [2, 1]]]
答案 4 :(得分:0)
在这一点上,这个问题已经提出了5年,但是我发现给出的答案比给出的答案要好一些(这很有帮助)。此答案说明了较大的二维数组。
from itertools import permutations, chain
from pprint import pprint
d = np.array([[1, 1], [0, 0]])
pprint([np.array(i).reshape(d.shape).tolist() for i in set(permutations(chain.from_iterable(d)))])
输出:
[[[1, 1], [0, 2], [2, 0]],
[[1, 0], [1, 0], [2, 2]],
[[1, 0], [0, 2], [1, 2]],
[[1, 2], [1, 0], [2, 0]],
[[1, 0], [1, 2], [0, 2]],
[[2, 1], [0, 2], [1, 0]],
[[2, 1], [0, 0], [2, 1]],
[[1, 2], [0, 2], [1, 0]],
[[2, 0], [0, 2], [1, 1]],
[[2, 1], [0, 1], [0, 2]],
[[1, 1], [2, 0], [0, 2]],
[[2, 0], [1, 0], [1, 2]],
[[1, 0], [2, 2], [0, 1]],
[[1, 2], [0, 1], [0, 2]],
[[0, 2], [2, 0], [1, 1]],
...