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
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
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