Shuffle two list at once with same order Shuffle two list at once with same order python python

Shuffle two list at once with same order


You can do it as:

import randoma = ['a', 'b', 'c']b = [1, 2, 3]c = list(zip(a, b))random.shuffle(c)a, b = zip(*c)print aprint b[OUTPUT]['a', 'c', 'b'][1, 3, 2]

Of course, this was an example with simpler lists, but the adaptation will be the same for your case.

Hope it helps. Good Luck.


I get a easy way to do this

import numpy as npa = np.array([0,1,2,3,4])b = np.array([5,6,7,8,9])indices = np.arange(a.shape[0])np.random.shuffle(indices)a = a[indices]b = b[indices]# a, array([3, 4, 1, 2, 0])# b, array([8, 9, 6, 7, 5])


from sklearn.utils import shufflea = ['a', 'b', 'c','d','e']b = [1, 2, 3, 4, 5]a_shuffled, b_shuffled = shuffle(np.array(a), np.array(b))print(a_shuffled, b_shuffled)#random output#['e' 'c' 'b' 'd' 'a'] [5 3 2 4 1]