Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use random.shuffle() on a generator? python

Tags:

How do I use random.shuffle() on a generator without initializing a list from the generator? Is that even possible? if not, how else should I use random.shuffle() on my list?

>>> import random >>> random.seed(2) >>> x = [1,2,3,4,5,6,7,8,9] >>> def yielding(ls): ...     for i in ls: ...             yield i ...  >>> for i in random.shuffle(yielding(x)): ...     print i ...  Traceback (most recent call last):   File "<stdin>", line 1, in <module>   File "/usr/lib/python2.7/random.py", line 287, in shuffle     for i in reversed(xrange(1, len(x))): TypeError: object of type 'generator' has no len() 

Note: random.seed() was designed such that it returns the same output after each script run?

like image 615
alvas Avatar asked Jan 17 '14 13:01

alvas


People also ask

How do you shuffle random in Python?

To shuffle strings or tuples, use random. sample() , which creates a new object. random. sample() returns a list even when a string or tuple is specified to the first argument, so it is necessary to convert it to a string or tuple.

How do you randomly shuffle elements in an array Python?

Shuffle an Array in Python Using the random.The random. shuffle() method takes a sequence as input and shuffles it. The important thing to note here is that the random. shuffle() does not return a new sequence as output but instead shuffles the original sequence.

What is the use of shuffle () function?

The shuffle() function randomizes the order of the elements in the array. This function assigns new keys for the elements in the array.

Which function is used to shuffle in Python?

shuffle() function in Python. The shuffle() is an inbuilt method of the random module. It is used to shuffle a sequence (list).


Video Answer


2 Answers

In order to shuffle the sequence uniformly, random.shuffle() needs to know how long the input is. A generator cannot provide this; you have to materialize it into a list:

lst = list(yielding(x)) random.shuffle(lst) for i in lst:     print i 

You could, instead, use sorted() with random.random() as the key:

for i in sorted(yielding(x), key=lambda k: random.random()):     print i 

but since this also produces a list, there is little point in going this route.

Demo:

>>> import random >>> x = [1,2,3,4,5,6,7,8,9] >>> sorted(iter(x), key=lambda k: random.random()) [9, 7, 3, 2, 5, 4, 6, 1, 8] 
like image 132
Martijn Pieters Avatar answered Oct 16 '22 10:10

Martijn Pieters


It's not possible to randomize the yield of a generator without temporarily saving all the elements somewhere. Luckily, this is pretty easy in Python:

tmp = list(yielding(x)) random.shuffle(tmp) for i in tmp:     print i 

Note the call to list() which will read all items and put them into a list.

If you don't want to or can't store all elements, you will need to change the generator to yield in a random order.

like image 32
Aaron Digulla Avatar answered Oct 16 '22 10:10

Aaron Digulla