Created
December 5, 2013 20:46
-
-
Save cwidmer/7813575 to your computer and use it in GitHub Desktop.
Shuffle several iterables the same way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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