本文介绍了获取一个numpy数组的所有排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个numpy数组[0,1,1,2,2,0,1,...],其中仅包含数字0-k.我想创建一个新数组,其中包含0-k排列的n个可能的数组.一个小例子,其中k = 2和n = 6:
I have a numpy array [0, 1, 1, 2, 2, 0, 1, ...] which only contains the numbers 0-k. I would like to create a new array that contains the n possible arrays of permutations of 0-k. A small example with k=2 and n=6:
a = [0, 1, 0, 2] permute(a) result = [[0, 1, 0, 2] [0, 2, 0, 1] [1, 0, 1, 2] [2, 1, 2, 0] [1, 2, 1, 0] [2, 0, 2, 1]]有人对如何实现这一目标有任何想法/解决方案吗?
Does anyone have any ideas/solutions as to how one could achieve this?
推荐答案您的 a 被组合主义者称为 multiset . 符号 库具有使用各种例程.
Your a is what combinatorists call a multiset. The sympy library has various routines for working with them.
>>> from sympy.utilities.iterables import multiset_permutations >>> import numpy as np >>> a = np.array([0, 1, 0, 2]) >>> for p in multiset_permutations(a): ... p ... [0, 0, 1, 2] [0, 0, 2, 1] [0, 1, 0, 2] [0, 1, 2, 0] [0, 2, 0, 1] [0, 2, 1, 0] [1, 0, 0, 2] [1, 0, 2, 0] [1, 2, 0, 0] [2, 0, 0, 1] [2, 0, 1, 0] [2, 1, 0, 0]更多推荐
获取一个numpy数组的所有排列
发布评论