Skip to content
Snippets Groups Projects
Commit 50eb9f8b authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

add random.shuffle_together

parent cd038e7d
No related branches found
No related tags found
No related merge requests found
# v2.7.47
added `random.shuffle_together`
......@@ -24,6 +24,7 @@ Visit the project's page at GitHub_!
posix
import
files
random
time
exceptions
processes
......
shuffle_together
================
.. autofunction:: satella.random.shuffle_together
__version__ = '2.7.47_a1'
__version__ = '2.7.47'
import random
import typing as tp
__all__ = ['shuffle_together']
def shuffle_together(*args: tp.Sequence) -> tp.List:
"""
args, being sequences of equal length, will be permuted in such a way
that their indices will still correspond to each other.
So given:
>>> a = [1, 2, 3]
>>> b = ['a', 'b', 'c']
>>> c = permute_together(a, b)
Might equal
>>> c == ([3, 1, 2], ['c', 'a', 'b'])
"""
indices = list(range(len(args[0])))
random.shuffle(indices)
return [[arg[i] for arg, i in zip(arg, indices)] for arg in args]
import unittest
from satella.random import shuffle_together
class TestRandom(unittest.TestCase):
def test_random(self):
a = [(1, 2, 3), ('a', 'b', 'c')]
b = zip(shuffle_together(*a))
self.assertEqual(set(b), {(1, 'a'), (2, 'b'), (3, 'c')})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment