I was able to repeat it in Python: lambda

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,

What do you do with iterative processing in a lambda expression?

Results of various trials:

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

I was able to repeat it in Python: lambda
I was addicted to scraping with Selenium (+ Python) in 2020
I was able to mock AWS-Batch with python, moto, so I will leave it
I want to be able to run Python in VS Code
I tried to implement PLSA in Python
I tried to implement permutation in Python
I tried to implement PLSA in Python 2
[Python / AWS Lambda layers] I want to reuse only module in AWS Lambda Layers
I wanted to solve ABC159 in Python
I tried to implement PPO in Python
I was addicted to confusing class variables and instance variables in Python
What I was addicted to Python autorun
I was able to print the thermal printer "PAPERANG" from Python (Windows10, Python3.6)
I want to do it with Python lambda Django, but I will stop
When I tried to run Python, it was skipped to the Microsoft Store
Use Python from Java with Jython. I was also addicted to it.
The file name was bad in Python and I was addicted to import
[Python] I was addicted to not saving internal variables of lambda expressions
I want to do Dunnett's test in Python
[Introduction to json] No, I was addicted to it. .. .. ♬
I want to create a window in Python
When I try matplotlib in Python, it says'cairo.Context'
I wrote "Introduction to Effect Verification" in Python
I want to merge nested dicts in Python
I tried to implement TOPIC MODEL in Python
I tried to implement selection sort in python
I want to display the progress in Python!
Summary of points I was addicted to running Selenium on AWS Lambda (python)
I want to write in Python! (1) Code format check
I tried to graph the packages installed in Python
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I want to write in Python! (2) Let's write a test
I tried to implement a pseudo pachislot in Python
I want to randomly sample a file in Python
I want to work with a robot in python.
I tried to implement GA (genetic algorithm) in Python
I want to write in Python! (3) Utilize the mock
I want to AWS Lambda with Python on Mac!
Deep nesting in Python makes it hard to read
I tried to summarize how to use pandas in python
I wondered if Python 3.4 was faster, but it was slower
I was addicted to trying logging.getLogger in Flask 1.1.x
I want to use the R dataset in python
What I was addicted to when using Python tornado
I want to do something in Python when I finish
I want to manipulate strings in Kotlin like Python!
It was a life I wanted to OCR on AWS Lambda to locate the characters.
I want to replace the variables in the python template file and mass-produce it in another file.
A beginner tried coloring line art with chainer. I was able to do it.
[Python] Scraping in AWS Lambda
Love was born in Python 2
Login to website in Python
Speech to speech in python [text to speech]
How to develop in Python
I understand Python in Japanese!
Love was born in Python 3
How to use Python lambda
Post to Slack in Python
I want to be able to analyze data with Python (Part 3)
Try to make it using GUI and PyQt in Python