shuffle two arrays the same way python

Solutions on MaxInterview for shuffle two arrays the same way python by the best coders in the world

showing results for - "shuffle two arrays the same way python"
Martín
18 Aug 2018
1# Example usage using random:
2import random
3# Say you want to shuffle (randomly reorder) the following lists in the
4# same way (e.g. because there's an association between the elements that
5# you want to maintain):
6your_list_1 = ['the', 'original', 'order']
7your_list_2 = [1, 2, 3]
8
9# Steps to shuffle:
10joined_lists = list(zip(your_list_1, your_list_2))
11random.shuffle(joined_lists) # Shuffle "joined_lists" in place
12your_list_1, your_list_2 = zip(*joined_lists) # Undo joining
13print(your_list_1)
14print(your_list_2)
15--> ('the', 'order', 'original') # Both lists shuffled in the same way
16--> (1, 3, 2) # Use list(your_list_2) to convert to list
Sofia
13 Feb 2020
1def unison_shuffled_copies(a, b):
2    assert len(a) == len(b)
3    p = numpy.random.permutation(len(a))
4    return a[p], b[p]
5
Yannik
26 Sep 2020
1>>> import numpy as np
2>>> x = np.arange(10)
3>>> y = np.arange(9, -1, -1)
4>>> x
5array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
6>>> y
7array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
8>>> s = np.arange(x.shape[0])
9>>> np.random.shuffle(s)
10>>> s
11array([9, 3, 5, 2, 6, 0, 8, 1, 4, 7])
12>>> x[s]
13array([9, 3, 5, 2, 6, 0, 8, 1, 4, 7])
14>>> y[s]
15array([0, 6, 4, 7, 3, 9, 1, 8, 5, 2])
16
queries leading to this page
how to shuffle two lists in pythonhow to shuffle 2 arrays the same wayshuffle two list in the same way pythonshuffle two arrays in same order pythonrandomly merging two numpy arraysnumpy shuffle two arrays with sameshuffle two numpy arrays in same orderpython randomize 2 lists equallyshuffle 2 lists pythonhow to shuffle two np arrays pythonnumpy arrays shuffle simultaneouslyshuffle two array together pythonshuffle two lists together python numpynumpy random shuffle 2 arrayspython shuffer np array the same wayhow to shuffle two numpy arrays with same ordernumpy shuffle two arrays in the same wayshuffle two arrays the same way numpyhow to shuffle 2 arrays so the index is the same pythonnumpy randomly reorder two arrayshow to randomly interchange elements of two numpy arrayhow to shuffle two lists the same way pythonshuffle numpy two arraysrandomly merge 4 numpy arrays into 2 arrayspython random shuffle two listshow to shuffle 2 arrays so the index is the sameshuffle two lists at the same time python 3shuffle 2 numpy arrays togethernumpy shuffle 2 arrays the same wayshuffle two lists in the same order pythonnumpy shuffle two arrays togethernumpy shuffle arrays togetherhow to shuffle np array two array in same wayhow to shuffle multiple arrays in same ordernp shuffle two arraysshuffle numpy arrays in same ordershuffle rows of two arrays simultaneouslyshuffle 2 lists python in the same wayshuffle 2 arrays simiultaneouslyhow to shuffle two arrays whilst keeping indicies same pythonequally shuffle in numpyshuffle two lists in the same orderpython shuffle two arrays the same wayshuffle two arrays in the same wayshuffle two numpy arrays togetherhow to shuffle and x and y in numpy array toghthershuffled array as a index for another array numpyshuffle two lists togetherrandom shuffle two listsrandomize two numpy arrays togetherconcatenate and shuffle two numpy arrayspython shuffle numpy arrays and list the same wayshuffle two lists the same way pythonhow to shuffle 2 arrays in the same way with pytjonnumpy shuffle zipshuffle two arrays in the same way pythonshuffle 2 arrays in the same ordernp random shuffle with two arraysnp shuffle two arrayshow to shuffle two numpy arrays togetherhow to shuffle two different arrays with the same length the same wayhow to exchange random element from two np array in pythonhow to shuffle np array two arraypython shuffle two lists togetherrandomly shuffle 2 lists the same waymultiplying two arrays in pythonshuffle two matrix with same order in pythonhow to shuffle two arrays simultaneouslynumpy random shuffle multiple arrayshow to shuffle 2 numpy arraysnumpy shuffle two arraysshuffle two arrays wame way pythonshuffle two lists together to oneshuffle two arrays together pythonsame random shuffling in two numpy arraysshuffle two np arrays togethershuffle two lists together pythonpython shuffle 2 lists combine and shuffle two lists in pythonshuffle 2 arrays in the same wayhow to shuffle two arrays pythonpython shuffle 2 lists togethernumpy shuffle two arrays in same wayhow to shuffle two lists having same ordershuffle two vectors the same numpynp shuffle 2 arrayqshuffle two numpy arraysshuffle two arrays numpyshuffle 2 listsrandom shuffle two arrayspython shuffle two lists in the same wayshuffle two lists the same pythonshuffle two numpy array reindex 28 29 shuffle two np arrays togetherpython3 shuffle to lists the samehow to shuffle two arrays the same way pytorchshuffle 2 lists the sameshuffle python avec 2 lists the same wayshuffled array as a index for ampther arrayshuffle multiple lists pythonrandomize elements between two numpy arrays pythonnp random shuffle with two matrixhot to shuffle multiple arrays in same ordermultiplying two arrays pythonpython randomize 2 lists equally one linenumpy shuffle two arrays the same wayhow to shuffle two arrays the same way pythonshuffle 2 arrays pythonpython shuffle two lists the same wayhow to shuffle two arrays simultaneously in pythonnumpy shuffle between two indexesnp shuffle two arrays the same waynumpy shuffle 2 arrays togetherpython shuffle 2 arrays in the same wayshuffle two arrays the same way pythonhow numpy array insert others numpy arrays with different dimensionshow to shuffle 2 arrays in the same way with pythonpython shuffle array with 2shuffle two arrays in same order in pythonshuffle two np array with same random stateshuffle two numpy arrays in orderpython shuffle two lists at once with same ordershuffle two arrays the same way python