Permute list of lists with mixed elements (np.random.permutation() fails with ValueError)
<p>I'm trying to permute a list composed of sublists with mixed-type elements:</p>

<pre><code>import numpy as np

a0 = ['122', 877.503017, 955.471176, [21.701201, 1.315585]]
a1 = ['176', 1134.076908, 1125.504758, [19.436181, 0.9987899]]
a2 = ['177', 1038.686843, 1018.987868, [19.539959, 1.183997]]
a3 = ['178', 878.999081, 1022.050447, [19.6448771, 1.1867719]]

a = [a0, a1, a2, a3]

b = np.random.permutation(a)
</code></pre>

<p>This will fail with:</p>

<pre><code>ValueError: cannot set an array element with a sequence
</code></pre>

<p>Is there a built in function that will allow me to generate such permutation?</p>

<p>I need to generate a single random permutation, I'm not trying to obtain all the possible permutations.</p>

<hr>

<p>I checked the three answers given with:</p>

<pre><code>import time
import random

# np.random.permutation()
start = time.time()
for _ in np.arange(100000):
    b = np.random.permutation([np.array(i, dtype='object') for i in a])
print(time.time() - start)

# np.random.shuffle()
start = time.time()
for _ in np.arange(100000):
    b = a[:]
    np.random.shuffle(b)
print(time.time() - start)

# random.shuffle()
start = time.time()
for _ in np.arange(100000):
    random.shuffle(a)
print(time.time() - start)
</code></pre>

<p>The results are:</p>

<pre><code>1.47580695152
0.11471414566
0.26300907135
</code></pre>

<p>so the <code>np.random.shuffle()</code> solution is about 10x faster than <code>np.random.permutation()</code> and 2x faster than <code>random.shuffle()</code>.</p>

<p>If you just want to create a random permutation of <code>a = [a0, a1, a2, a3]</code>, might I suggest permuting the indices instead?</p>

<pre><code>&gt;&gt;&gt; random_indices = np.random.permutation(np.arange(len(a)))
&gt;&gt;&gt; a_perm = [a[i] for i in random_indices]
... # Or just use the indices as you see fit...
</code></pre>

<p>If you're using numpy <em>just</em> for this, skip numpy altogether instead and just use <a href="https://docs.python.org/3.5/library/random.html#random.shuffle" rel="nofollow"><code>random.shuffle</code></a> to effect the same:</p>

<pre><code>&gt;&gt;&gt; import random
&gt;&gt;&gt; random.shuffle(a)
</code></pre>

python numpy permutation