[PYTHON] Fix the argument of the function used in map

How to fix function arguments

When using map, you often want to fix only some arguments, so make a note of it.

Use functools.partial. This object behaves like a function called with arguments.

Consider the case of creating a function that adds two arguments and returns them, and then fixing only one argument.

In [1]: import functools

In [2]: def f(a, b):
   ...:         return a + b
   ...:

In [3]: map(functools.partial(f, b=1), [1, 2, 3])
Out[3]: [2, 3, 4]

reference

functools.partial

Recommended Posts

Fix the argument of the function used in map
Get the caller of a function in Python
Used from the introduction of Node.js in WSL environment
[Golang] Specify an array in the value of map
I examined the argument class_weight of Chainer's softmax_cross_entropy function.
Difference in results depending on the argument of multiprocess.Process
Note: The meaning of specifying only * (asterisk) as an argument in the Python function definition.
Have the equation graph of the linear function drawn in Python
[Python3] Call by dynamically specifying the keyword argument of the function
Make the function of drawing Japanese fonts in OpenCV general
Function argument type definition in python
The story of participating in AtCoder
Implementation of login function in Django
The story of the "hole" in the file
The meaning of ".object" in Django
Try transcribing the probability mass function of the binomial distribution in Python
If you give a list with the default argument of the function ...
A function that measures the processing time of a method in python
Create a function to get the contents of the database in Go
Verify the compression rate and time of PIXZ used in practice
Explanation and implementation of the XMPP protocol used in Slack, HipChat, and IRC
Specify the color in the matplotlib 2D map
Check the behavior of destructor in Python
Summary of methods often used in pandas
The story of an error in PyOCR
Summary of frequently used commands in matplotlib
Predict the amount of electricity used in 2 days and publish it in CSV
[Linux] List of Linux commands used in practice
Implement part of the process in C ++
OR the List in Python (zip function)
The result of installing python in Anaconda
Let's claim the possibility of pyenv-virtualenv in 2021
Look up the names and data of free variables in function objects
About the arguments of the setup function of PyCaret
[Android] Display images on the web in the info Window of Google Map
The basics of running NoxPlayer in Python
Why is cross entropy used for the objective function of the classification problem?
Difference in output of even-length window function
Open an Excel file in Python and color the map of Japan
In search of the fastest FizzBuzz in Python
[Introduction to Python] Thorough explanation of the character string type used in Python!
Make the default value of the argument immutable
Python> function> Example of taking function as an argument> map (lambda x: 2 ** x, [1, 2, 3]) / locals () ['myprint'] (3.1415, 2.718)
[Linux] Difference in time information depending on the clock ID of the clock_gettime () function
Python scikit-learn A collection of predictive model tips often used in the field
Understand the images of various matrix operations used in Keras (Tensorflow) with examples
[AWS] Let's run a unit test of Lambda function in the local environment
Set an upper limit on the number of recursive function iterations in Python
Python scikit-learn A collection of predictive model tips often used in the field
The _authenticate_with_backend function was obsolete in django auth.autenticate
Output the number of CPU cores in Python
I tried the pivot table function of pandas
Draw a graph of a quadratic function in Python
The meaning of {version-number} in the mysql rpm package
I tried cluster analysis of the weather map
[Python] Sort the list of pathlib.Path in natural sort
Change the font size of the legend in df.plot
Defeat the probability density function of the normal distribution
Match the distribution of each group in Python
Summary of what was used in 100 Pandas knocks (# 1 ~ # 32)
Why the Python implementation of ISUCON 5 used Bottle