Reference article: All elements move (do not remain in the same position) shuffle I thought it would be rude to comment on other people's articles so much, so I made another article.
I wrote something like this. A function that returns a shuffled list.
python
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
I wondered what would happen if I wrote this in a lambda expression.
by the way,
Results of various trials:
. If it is `` ()
`, it becomes a generator and each element is not evaluated.)It was that.
python
import random
shuffle_all_move = ( lambda items :
( lambda
length = len( items )
, res = [ 0 ] * len( items )
, rand_ord = random.sample( range( len( items ) ), k=len( items ) )
:
(
[ ( res.pop( rand_ord[ i ] )
, res.insert(rand_ord[ i ], items[ rand_ord[ ( i + 1 ) % length ] ])
) for i in range( length )
]
, res
)[-1]
)()
)
Recommended Posts