Skip to content

Instantly share code, notes, and snippets.

@cwidmer
Created December 5, 2013 20:46
Show Gist options
  • Save cwidmer/7813575 to your computer and use it in GitHub Desktop.
Save cwidmer/7813575 to your computer and use it in GitHub Desktop.
Shuffle several iterables the same way
import random
def coshuffle(*args):
"""
will shuffle target_list and apply
same permutation to other lists
>>> helper.coshuffle([2, 1, 3], [4, 2, 8], [6, 3, 12])
([5, 3, 2, 1, 4], [5, 3, 2, 1, 4], [5, 3, 2, 1, 4])
"""
assert len(args) > 0, "need at least one list"
num_elements = len(args[0])
for arg in args:
assert len(arg) == num_elements, "length mismatch"
idx = range(num_elements)
random.shuffle(idx)
new_lists = []
for arg in args:
new_lists.append([arg[i] for i in idx])
return tuple(new_lists)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment