Random shuffle list python

Shuffle an array with python, randomize array item order with python

@Tushar Despite the name, the object you get from np.array() is not an «array» in the sense of this question. You may want to look for another question to find out how to shuffle a Numpy array specifically. (Or you can search the web to find the right page in the Numpy documentation.)

import random random.shuffle(array) 

Alternative way to do this using sklearn

from sklearn.utils import shuffle X=[1,2,3] y = ['one', 'two', 'three'] X, y = shuffle(X, y, random_state=0) print(X) print(y) 

Advantage: You can random multiple arrays simultaneously without disrupting the mapping. And ‘random_state’ can control the shuffling for reproducible behavior.

Just in case you want a new array you can use sample :

import random new_array = random.sample( array, len(array) ) 

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:

 import random def my_shuffle(array): random.shuffle(array) return array 

Then you can do lines like:

 for suit in my_shuffle(['hearts', 'spades', 'clubs', 'diamonds']): 

It doesn’t return anything specifically because it is trying to remind you that it works by altering the input in place. (This can save memory.) Your function alters its input in place also.

I guess it’s a style thing. Personally I prefer the fact that I can write a single line to achieve what would take a couple otherwise. It seems odd to me that a language which aims to allow programs to be as short as possible doesn’t tend to return the passed object in these cases. Since it alters the input in place, you can replace a call to random.shuffle for a call to this version without issue.

Читайте также:  Python setup install package files

Python doesn’t actually aim to be as brief as possible. Python aims to balance readability with expressivity. It so happens to be fairly brief, mainly because it is a very high-level language. Python’s own built-ins typically (not always) strive to either be «functionlike» (return a value, but don’t have side effects) or be «procedurelike» (operate via side effects, and don’t return anything). This goes hand-in-hand with Python’s quite strict distinction between statements and expressions.

Maybe, but this could be premature optimization (it could be helpful, but the need to shuffle doesn’t explicitly require the need to return the array). Also, shuffle(array) followed by some use of shuffle would only be 2 lines as opposed to 3 + n (times usage), although I guess it would be a saving if you use it many times. Here is a great video that discusses this type of thing (e.g. phantom requirements and premature optimisation) — pyvideo.org/video/880/stop-writing-classes

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.

You can sort your array with random key

sorted(array, key = lambda x: random.random()) 

key only be read once so comparing item during sort still efficient.

but look like random.shuffle(array) will be faster since it written in C

In addition to the previous replies, I would like to introduce another function.

numpy.random.shuffle as well as random.shuffle perform in-place shuffling. However, if you want to return a shuffled array numpy.random.permutation is the function to use.

I don’t know I used random.shuffle() but it return ‘None’ to me, so I wrote this, might helpful to someone

def shuffle(arr): for n in range(len(arr) - 1): rnd = random.randint(0, (len(arr) - 1)) val1 = arr[rnd] val2 = arr[rnd - 1] arr[rnd - 1] = val1 arr[rnd] = val2 return arr 

yes it returns None, but array is modifed, if you really want to return something then do this import random def shuffle(arr): random.shuffle(arr) return arr

Be aware that random.shuffle() should not be used on multi-dimensional arrays as it causes repetitions.

Imagine you want to shuffle an array along its first dimension, we can create the following test example,

import numpy as np x = np.zeros((10, 2, 3)) for i in range(10): x[i, . ] = i*np.ones((2,3)) 

so that along the first axis, the i-th element corresponds to a 2×3 matrix where all the elements are equal to i.

If we use the correct shuffle function for multi-dimensional arrays, i.e. np.random.shuffle(x) , the array will be shuffled along the first axis as desired. However, using random.shuffle(x) will cause repetitions. You can check this by running len(np.unique(x)) after shuffling which gives you 10 (as expected) with np.random.shuffle() but only around 5 when using random.shuffle() .

Источник

Shuffling a list of objects [duplicate]

Can you give an example how it fails? random.shuffle should work invariant to the type of the objects in the list.

As stated below, random.shuffle doesn’t return a new shuffled list; it shuffles the list in place. So you shouldn’t say «print random.shuffle(b)» and should instead do the shuffle on one line and print b on the next line.

Why did you try to print the output of shuffle ? It should be None as it is shuffling the array in place

26 Answers 26

random.shuffle should work. Here’s an example, where the objects are lists:

from random import shuffle x = [[i] for i in range(10)] shuffle(x) print(x) # print(x) gives [[9], [2], [7], [0], [4], [5], [3], [1], [8], [6]] 

Note that shuffle works in place, and returns None .

More generally in Python, mutable objects can be passed into functions, and when a function mutates those objects, the standard is to return None (rather than, say, the mutated object).

@seokhoonlee Neither. It is a pseduo-random number generator which, when possible, is seeded by a source of real randomness from the OS. For all but cryptography purposes it is random «enough». This is laid out in detail in the random module’s documentation.

@CharlieParker: Not that I know of. You could use random.sample(x, len(x)) or just make a copy and shuffle that. For list.sort which has a similar issue, there’s now list.sorted , but there’s not a similar variant for shuffle .

@seokhonlee for crypto-secure randomness, use from random import SystemRandom instead; add cryptorand = SystemRandom() and change row 3 to cryptorand.shuffle(x)

As you learned the in-place shuffling was the problem. I also have problem frequently, and often seem to forget how to copy a list, too. Using sample(a, len(a)) is the solution, using len(a) as the sample size. See https://docs.python.org/3.6/library/random.html#random.sample for the Python documentation.

Here’s a simple version using random.sample() that returns the shuffled result as a new list.

import random a = range(5) b = random.sample(a, len(a)) print a, b, "two list same:", a == b # print: [0, 1, 2, 3, 4] [2, 1, 3, 4, 0] two list same: False # The function sample allows no duplicates. # Result can be smaller but not larger than the input. a = range(555) b = random.sample(a, len(a)) print "no duplicates:", a == list(set(b)) try: random.sample(a, len(a) + 1) except ValueError as e: print "Nope!", e # print: no duplicates: True # print: Nope! sample larger than population 

Источник

Оцените статью