Answers for "shuffle two list in the same way python"

2

python shuffle two lists in the same way

# Example usage using random:
import random
# Say you want to shuffle (randomly reorder) the following lists in the
# same way (e.g. because there's an association between the elements that
# you want to maintain):
your_list_1 = ['the', 'original', 'order']
your_list_2 = [1, 2, 3]

# Steps to shuffle:
joined_lists = list(zip(your_list_1, your_list_2))
random.shuffle(joined_lists) # Shuffle "joined_lists" in place
your_list_1, your_list_2 = zip(*joined_lists) # Undo joining
print(your_list_1)
print(your_list_2)
--> ('the', 'order', 'original') # Both lists shuffled in the same way
--> (1, 3, 2) # Use list(your_list_2) to convert to list
Posted by: Guest on November-11-2020
2

shuffle two arrays the same way python

>>> import numpy as np
>>> x = np.arange(10)
>>> y = np.arange(9, -1, -1)
>>> x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> y
array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
>>> s = np.arange(x.shape[0])
>>> np.random.shuffle(s)
>>> s
array([9, 3, 5, 2, 6, 0, 8, 1, 4, 7])
>>> x[s]
array([9, 3, 5, 2, 6, 0, 8, 1, 4, 7])
>>> y[s]
array([0, 6, 4, 7, 3, 9, 1, 8, 5, 2])
Posted by: Guest on April-09-2020

Code answers related to "shuffle two list in the same way python"

Python Answers by Framework

Browse Popular Code Answers by Language