shuffle

shuffling a list with restrictions in Python

shuffling a list with restrictions in Python Question: I have a problem with randomizing a list with restrictions in Python (3). I have seen a few other questions relating to this, but none of them really seem to solve my problem. I’m a beginner, so any help is much appreciated! I’m designing an experiment using …

Total answers: 5

Numpy shuffle multidimensional array by row only, keep column order unchanged

Numpy shuffle multidimensional array by row only, keep column order unchanged Question: How can I shuffle a multidimensional array by row only in Python (so do not shuffle the columns). I am looking for the most efficient solution, because my matrix is very huge. Is it also possible to do this highly efficient on the …

Total answers: 5

Shuffle DataFrame rows

Shuffle DataFrame rows Question: I have the following DataFrame: Col1 Col2 Col3 Type 0 1 2 3 1 1 4 5 6 1 … 20 7 8 9 2 21 10 11 12 2 … 45 13 14 15 3 46 16 17 18 3 … The DataFrame is read from a CSV file. All …

Total answers: 13

Best way to permute contents of each column in numpy

Best way to permute contents of each column in numpy Question: What’s the best way to efficiently permute the contents of each column in a numpy array? What I have is something like: >>> arr = np.arange(16).reshape((4, 4)) >>> arr array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], …

Total answers: 3

Shuffle two list at once with same order

Shuffle two list at once with same order Question: I’m using the nltk library’s movie_reviews corpus which contains a large number of documents. My task is get predictive performance of these reviews with pre-processing of the data and without pre-processing. But there is problem, in lists documents and documents2 I have the same documents and …

Total answers: 8

Why does random.shuffle return None?

Why does random.shuffle return None? Question: Why is random.shuffle returning None in Python? >>> x = [‘foo’,’bar’,’black’,’sheep’] >>> from random import shuffle >>> print shuffle(x) None How do I get the shuffled value instead of None? Asked By: alvas || Source Answers: random.shuffle() changes the x list in place. Python API methods that alter a …

Total answers: 5

Python shuffle such that position will never repeat

Python shuffle such that position will never repeat Question: I’d like to do a random shuffle of a list but with one condition: an element can never be in the same original position after the shuffle. Is there a one line way to do such in python for a list? Example: list_ex = [1,2,3] each …

Total answers: 6

shuffle vs permute numpy

shuffle vs permute numpy Question: What is the difference between numpy.random.shuffle(x) and numpy.random.permutation(x)? I have read the doc pages but I could not understand if there was any difference between the two when I just want to randomly shuffle the elements of an array. To be more precise suppose I have an array x=[1,4,2,8]. If …

Total answers: 4

Better way to shuffle two related lists

Better way to shuffle two related lists Question: Is there better ways to randomly shuffle two related lists without breaking their correspondence in the other list? I’ve found related questions in numpy.array and c# but not exactly the same one. As a first try, a simple zip trick will do: import random a = [[1, …

Total answers: 8

Better way to shuffle two numpy arrays in unison

Better way to shuffle two numpy arrays in unison Question: I have two numpy arrays of different shapes, but with the same length (leading dimension). I want to shuffle each of them, such that corresponding elements continue to correspond — i.e. shuffle them in unison with respect to their leading indices. This code works, and …

Total answers: 18