lundi 30 septembre 2019

How to shuffle multiple iterables in same order?

I seek a plain Python function that accepts an arbitrary number of iterables (tuples, lists, dictionaries), and returns them shuffled in the same order:

a = (1, 2, {3: 4}, 5})
b = [(5,6), [7,8], [9,0], [1,2]]
c = {'arrow': 5, 'knee': 'guard', 0: ('x',2)}

x, y, z = magic(a, b, c)
print(x, y, z, sep='\n')
# ({3: 4}, 1, 2)
# [[9, 0], (5, 6), [7, 8]]
# {0: ('x', 2), 'arrow': 5, 'knee': 'guard'}

The function must:

  • Return iterables shuffled in the same order (see above)
  • Accept any number of iterables
  • Preserve iterables types
  • Support nested iterables of any depth and type
  • Return iterables with length of shortest iterable's length w/o raising error (see above)

OK if using Numpy, random, etc for the shuffle step (e.g. np.random.shuffle(magic_packing)), but cannot be a high-level library method (uses multiprocessing, encoding, etc - should be 'plain')


I've seen related SO's, but could not adapt them to such a generalized case. How can this be accomplished?


Aucun commentaire:

Enregistrer un commentaire