final version? ?? http://qiita.com/ttatsf/items/d4543ccc80e0fbe3891d#comment-3eccf9c112d73c505bc2
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).
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
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
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