[Python] I was addicted to not saving internal variables of lambda expressions

As a list of basis functions when doing linear radial basis regression

bases = [lambda x: (torch.exp(-((x[..., 0] - x_mean)/std_x) ** 2 - ((x[..., 1] - dx_mean)/std_dx) ** 2))
         for x_mean in np.linspace(start=-1., stop=1., num=10)
         for dx_mean in np.linspace(start=-1., stop=1., num=10)
         ]

But I ended up with the same list of functions.

Think of a simple example

f = [lambda x: print(i) for i in range(4)]

Then the value of the variable $ i $ at runtime of the lambda statement is used, For any n

f[n](10) = 3

Will be.

To use the variable value at the time of definition,

f = [lambda x, s=i: print(s) for i in range(4)]

You can use the default arguments like this

Recommended Posts

[Python] I was addicted to not saving internal variables of lambda expressions
Summary of points I was addicted to running Selenium on AWS Lambda (python)
[Fixed] I was addicted to alphanumeric judgment of Python strings
What I was addicted to Python autorun
I was addicted to confusing class variables and instance variables in Python
I wrote AWS Lambda, and I was a little addicted to the default value of Python arguments
I was able to recurse in Python: lambda
[Introduction to Python] Basic usage of lambda expressions
I was addicted to scraping with Selenium (+ Python) in 2020
I was able to repeat it in Python: lambda
What I was addicted to when using Python tornado
What I was addicted to when migrating Processing users to Python
I was addicted to multiprocessing + psycopg2
A story that I was addicted to calling Lambda from AWS Lambda.
The story that the version of python 3.7.7 was not adapted to Heroku
What I was addicted to when introducing ALE to Vim for Python
What I was addicted to with json.dumps in Python base64 encoding
I was addicted to pip install mysqlclient
I was addicted to Flask on dotCloud
[Road to intermediate Python] Use lambda expressions
A story I was addicted to when inserting from Python to a PostgreSQL table
I was addicted to creating a Python venv environment with VS Code
Use Python from Java with Jython. I was also addicted to it.
I was addicted to not being able to use Markdown on pypi's long_description
The file name was bad in Python and I was addicted to import
[Lambda] I tried to incorporate an external module of python via S3
[Introduction to json] No, I was addicted to it. .. .. ♬
I thought it was the same as python, and I was addicted to the problem that the ruby interpreter did not start.
Three things I was addicted to when using Python and MySQL with Docker
AtCoder AGC 041 C --I was addicted to the full search of Domino Quality
I want to get / execute variables / functions / classes of external files from Python
Summary of studying Python to use AWS Lambda
A note I was addicted to when running Python with Visual Studio Code
A story that I was addicted to when I made SFTP communication with python
I was soberly addicted to calling awscli from a Python 2.7 script registered in crontab
Note that I was addicted to npm script not passing in the verification environment
A story that I was addicted to at np.where
[Python] I tried to get Json of squid ring 2
I was addicted to trying logging.getLogger in Flask 1.1.x
I tried to summarize the string operations of Python
When creating a pipenv environment, I got addicted to "Value Error: Not a valid python path"
I tried to find the entropy of the image with python
[IOS] GIF animation with Pythonista3. I was addicted to it.
I want to specify another version of Python with pyvenv
I want to start a lot of processes from python
Challenge problem 5 with Python: lambda ... I decided to copy without
[Python] I tried to visualize the follow relationship of Twitter
I want to know the features of Python and pip
I tried to implement blackjack of card game in Python
The story of when I was addicted to Caused by SSLError ("Can't connect to HTTPS URL because the SSL module is not available.")
[Python3] List of sites that I referred to when I started Python
I was a little addicted to installing Python3.3 + mod_wsgi3.4 on Sakura VPS (CentOS), so a retrospective memo
When I tried to scrape using requests in python, I was addicted to SSLError, so a workaround memo
When I tried to use pip with python, I was told that XML_SetHashSalt could not be found.
I was addicted to not being able to get an email address from google with django-allauth authentication
How to use Python lambda
The record I was addicted to when putting MeCab on Heroku
How to know the internal structure of an object in Python
I tried to make a regular expression of "amount" using Python
[Introduction to Python] I compared the naming conventions of C # and Python.
I tried to make a regular expression of "time" using Python