[PYTHON] All elements move (do not remain in the same position) shuffle

8/25 postscript

final version? ?? http://qiita.com/ttatsf/items/d4543ccc80e0fbe3891d#comment-3eccf9c112d73c505bc2

8/23 postscript

It was pointed out that the following program does not cover all shuffle patterns. An example of a strange exchange such as [0,1,2,3] → [1,0,3,2]. Please be careful when using it.

8/21 postscript

It seems that there are finally two lines (white eyes)

def shuffle_all_move(items):
    rand_ord = random.sample(range(len(items)), k=len(items))
    return [items[r] for i, r in sorted(zip(rand_ord, rand_ord[1:]+rand_ord[:1]))]

The execution speed seems to be slightly faster for the above code (measurement results are posted in the comment section).


8/19 postscript

I like the one made by ttatsf in the comment section. (The base "Sattro's algorithm" was introduced by 7of9) Thank you to everyone who wrote the code in the comments.

import random

def shuffle_all_move( items ):
  length = len( items )
  res = [ 0 ] * length
  rand_ord = random.sample( range( length ), k=length )
  for i in range(  length  ) :
    res[ rand_ord[ i ] ] = items[ rand_ord[ ( i + 1 ) % length ] ]
  return res

Really elegant! … So, I recommend using the above from the appropriate DIY shuffle below w


Original post

It is a python + numpy implementation of shuffle that absolutely moves elements (note that it is assumed that there are 3 or more elements in the array). procedure

  1. Randomly divide the element into 3 groups
  2. shuffle for each group
  3. Slide one group.

Random group division of 1 is actually realized by shuffling and then dividing the array into three. Even if you slide a group of shuffled arrays, it will be irrelevant to the position of the original array, so this shuffle will be restored later. This guarantees that it will always move to a different position than the original array.

This code borrows the code on the following two pages. ※ resettable_shuffle/reset_shuffle http://qiita.com/EafT/items/9527cb30409b70106fd4 ※ divide_list http://fits.hatenablog.com/entry/2015/11/09/211012

def shuffle_all_move(_array):
    array = _array.copy()
    seed = random.randint(0,np.iinfo(np.int32).max)
    resettable_shuffle(array,seed)
    array = divide_list(array,3)
    if type(array[0]) == list:
        array = array[-1] + array[0] + array[1]
    else:
        array = np.vstack((array[-1], array[0],array[1]))
    return reset_shuffle(array,seed)

Execution example

test_array = ['a','b','c','d','e','f']
print(test_array)
test_array = shuffle_all_move(test_array)
print(test_array)

['a', 'b', 'c', 'd', 'e', 'f'] ['c', 'e', 'f', 'b', 'd', 'a']

Recommended Posts

All elements move (do not remain in the same position) shuffle
Make sure all the elements in the list are the same in Python
[Python] Combine all the elements in the array
[Python] Outputs all combinations of elements in the list
Do not pass self to ProcessPoolExecutor in the class
Determine if all list elements are present in the dict key
[Python] Do not put Japanese in the path used by OpenCV