When dealing with regular Python lists, random.shuffle()
will do the job just as the previous answers show.
But when it come to ndarray
(numpy.array
), random.shuffle
seems to break the original ndarray
. Here is an example:
import random
import numpy as np
import numpy.random
a = np.array([1,2,3,4,5,6])
a.shape = (3,2)
print a
random.shuffle(a) # a will definitely be destroyed
print a
Just use: np.random.shuffle(a)
Like random.shuffle
, np.random.shuffle
shuffles the array in-place.