获取一个numpy数组的所有排列

编程入门 行业动态 更新时间:2024-10-26 20:24:21
本文介绍了获取一个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数组的所有排列

本文发布于:2023-11-30 10:09:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649609.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   排列   numpy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!