tags:

views:

3100

answers:

2

What's the easiest way to shuffle an array with python?

+24  A: 
import random
random.shuffle(array)
David Zaslavsky
Beat me by 47 seconds.
Douglas Leeder
Just be careful, shuffle was broken in earlier numpy versions (I think this still applies to the current Debian "stable"). If I remember correctly the bug only arose when shuffle was used on a >1d numpy array.
nikow
This deserves an http://letmegooglethatforyou.com/?q=python+shuffle. Oh, look it's the second hit.
S.Lott
@nikow: this has nothing to do with numpy. Just regular Python.
David Zaslavsky
@David: Oh right, sorry, I missed that. I got almost very badly bitten by this numpy bug, so that stuck to my mind.
nikow
+11  A: 
import random
random.shuffle(array)
Douglas Leeder
haha don't you hate that? Been beaten out a few times myself.
Triptych
lol, me too. Great minds think alike. +1
David Zaslavsky