What's the easiest way to shuffle an array with python?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
In addition to the previous replies, I would like to introduce another function.
numpy.random.shuffle
as well asrandom.shuffle
perform in-place shuffling. However, if you want to return a shuffled arraynumpy.random.permutation
is the function to use.The other answers are the easiest, however it's a bit annoying that the
random.shuffle
method doesn't actually return anything - it just sorts the given list. If you want to chain calls or just be able to declare a shuffled array in one line you can do:Then you can do lines like:
Just in case you want a new array you can use
sample
: