SHARE
TWEET

Untitled

a guest Oct 1st, 2019 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from time import time
  2. import functools
  3. import random
  4.  
  5. #########################################################
  6. a = (1, 2, {'knee': 4}, 5, 9, 10, 7, [8,9], 9, 10)
  7. b = [(5,6), [7,8], [9,'arrow'], [1,2], {'5': 6}, [6,0], 7, {8:9}, (9,10), 0]
  8. c = {'arrow': 5, 'knee': 'guard', 0: ('x',2), 'Python': ('dog',5),'Saitama': -10,
  9.      42: 'Life', 'Area': 51, 8: 8, '9': (1,1), 10: 'end'}
  10.  
  11. #########################################################
  12. def ordered_shuffle(*args):
  13.     _args      = [arg if type(arg)!=dict else arg.items() for arg in args]
  14.     args_split = [arg for arg in zip(*_args)]
  15.     args_shuffled = random.sample(args_split, len(args_split))
  16.     args_shuffled = map(tuple, zip(*args_shuffled))
  17.     return [type(args[i])(arg) for i, arg in enumerate(args_shuffled)]
  18.  
  19. @functools.singledispatch
  20. def shuffle(arg, order):
  21.     lst = list(arg)
  22.     return type(arg)(lst[i] for i in order)
  23.  
  24. @shuffle.register(dict)
  25. def _(arg, order):
  26.     item = list(arg.items())
  27.     return dict(item[i] for i in order)
  28.  
  29. #########################################################
  30. def ordered_shuffle2(*args):
  31.     min_length = min(map(len, args))
  32.     indices = random.sample(range(min_length), min_length)
  33.     return [shuffle(arg, indices) for arg in args]
  34.  
  35. ordered_shuffle3 = lambda *args:[type(args[i])(arg) for i, arg in
  36.             enumerate(map(tuple, zip(
  37.             *random.sample([y for y in zip(*[x if type(x)!=dict else x.items()
  38.                                          for x in args])], min(len(z) for z in args)
  39.                           ))))]
  40.    
  41. #########################################################
  42. def ordered_shuffle4(*args):
  43.     min_length = min(map(len, args))
  44.     idx = list(range(min_length))
  45.     random.shuffle(idx)
  46.     results = []
  47.     for arg in args:
  48.         if isinstance(arg, list):
  49.             results.append([arg[i] for i in idx])
  50.         elif isinstance(arg, tuple):
  51.             results.append(tuple(arg[i] for i in idx))
  52.         elif isinstance(arg, dict):
  53.             items = list(arg.items())
  54.             results.append(dict(items[i] for i in idx))
  55.         else:
  56.             raise ValueError(
  57.                 "Encountered", type(arg),
  58.                 "expecting only list, dict, or tuple"
  59.             )
  60.     return results # 155.7 195.3  160.45 131.87
  61.  
  62. #########################################################
  63. iters = int(1e7)
  64. t0 = time()
  65. for _ in range(iters):
  66.     ordered_shuffle(a, b, c)
  67. t1_bench = time() - t0
  68. print('ordered_shuffle: %s' % t1_bench)
  69.  
  70. t0 = time()
  71. for _ in range(iters):
  72.     ordered_shuffle2(a, b, c)
  73. t2_bench = time() - t0
  74. print('ordered_shuffle2: %s' % t2_bench)
  75.  
  76. t0 = time()
  77. for _ in range(iters):
  78.     ordered_shuffle3(a, b, c)
  79. t3_bench = time() - t0
  80. print('ordered_shuffle3: %s' % t3_bench)
  81.  
  82. t0 = time()
  83. for _ in range(iters):
  84.     ordered_shuffle4(a, b, c)
  85. t4_bench = time() - t0
  86. print('ordered_shuffle4: %s' % t4_bench)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top