擅长:python、mysql、java
<p>你的<strong>a</strong>是组合学家称之为a<em>多集</em>的。<strong><a href="https://docs.sympy.org/latest/" rel="nofollow noreferrer">sympy</a></strong>库有<a href="https://docs.sympy.org/latest/modules/combinatorics/permutations.html" rel="nofollow noreferrer">various routines</a>用于处理它们。</p>
<pre><code>>>> 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]
</code></pre>