[Road to intermediate Python] Use lambda expressions

Link to summary

https://qiita.com/ganariya/items/fb3f38c2f4a35d1ee2e8

Introduction

In order to study Python, I copied a swarm intelligence library called acopy.

In acopy, many interesting Python grammars and idioms are used, and it is summarized that it is convenient among them.

This time, you'll learn about Python lambda expressions.

What is a lambda expression?

Lambda expression is a concept used in many other programming languages and means ** anonymous function **. It is not necessary to give a name, ** when the scope to be used is very small **, the lambda expression is to define an anonymous function on the spot and throw it away **.

In C ++, it can be used with the notation []() {}.

Example of lambda expression

The lambda expression is expressed in the form of lambda argument: processing. The feature is that a value is always returned as a result of processing.

For example, let's create an add function that adds the arguments of two integers, and an lm_add function that expresses it as lambda.

def add(x, y):
    return x + y


lm_add = lambda x, y: x + y

x = 10
y = 40

'''
50
50
'''
print(add(x, y))
print(lm_add(x, y))

The lm_add function behaves the same as the add function. After receiving the arguments $ x and y $ and calculating $ x + y $, it is forcibly returned.

You can use variadic arguments just like regular functions.

f = lambda *args, **kwargs: print(args, kwargs)

'''
(10, 'a') {'hello': 'world'}
'''
f(10, 'a', hello='world')

Usage example

lambda expressions are basically disposable and don't need to be named, and are only used (and should be) in the smallest scopes.

For example

--As a sorting standard --As a standard for maximum value

It is often used in combination with built-in functions such as. Especially used in competitive programming.

For example, the following sorts an array of tuples in ascending order relative to the $ 1 $ element, and if they are the same, further sorts them in ascending order by the $ 0 $ element.

tuples = [(10, -3), (4, 10), (-4, 13), (-20, -50), (-20, 1), (0, 0), (-22, -50)]

tuples = sorted(tuples, key=lambda x: (x[1], x[0]))

'''
[(-20, -50), (-22, -50), (10, -3), (0, 0), (-20, 1), (4, 10), (-4, 13)]
'''
print(tuples)

Since it is not necessary to define the above comparison function outside the scope, and because the position of the function becomes far away and the burden of following the process is reduced, it seems better to define it with lambda.

Finally

I looked at lambda. Especially when doing competitive programming in Python. I would like to utilize lambda expressions in other languages as well.

Sorting that replaces $ 2 $ elements in Python (C ++ sorting, etc.) Is there no choice but to define ʻeq, lt` in the class ... (I hope it can be done with lambda)

References

-Python lambda is easy to understand

Recommended Posts

[Road to intermediate Python] Use lambda expressions
[Road to intermediate Python] Use ternary operators
A road to intermediate Python
How to use Python lambda
[Road to Intermediate] Understanding Python Properties
[Road to intermediate Python] Use if statement in list comprehension
How to use regular expressions in Python
[Road to intermediate Python] Article link summary
Summary of studying Python to use AWS Lambda
[Introduction to Python] Basic usage of lambda expressions
python3: How to use bottle (2)
[Python] How to use list 1
[Road to Python Intermediate] Define __getattr__ function in class
How to use Python argparse
Use regular expressions in Python
Python: How to use pydub
[Python] How to use checkio
Easy to use E-Cell 4 Intermediate
Re: Python lambda is useless ^ H ^ H ^ H ^ H ^ H Difficult to use
[Python] How to use input ()
[Python] How do you use lambda expressions? ?? [Scribbles] [Continued-1]
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
[Road to intermediate Python] Install packages in bulk with pip
[Road to intermediate Python] Enables comparison operations with original classes
[Introduction to Udemy Python 3 + Application] 58. Lambda
Python: How to use async with
[Python] How to use Pandas Series
How to use Requests (Python Library)
How to use SQLite in Python
[Lambda] [Python] Post to Twitter from Lambda!
Road to Linux Intermediate: Network Edition
[Introduction to Python] Let's use pandas
[Python] How to use list 3 Added
How to use Mysql in python
How to use OpenPose's Python API
How to use ChemSpider in Python
How to use FTP with Python
Python: How to use pydub (playback)
How to use PubChem in Python
Easy to use Jupyter notebook (Python3.5)
[Introduction to Python] Let's use pandas
How to use python zip function
Use PostgreSQL with Lambda (Python + psycopg2)
[Introduction to Python] Let's use pandas
[Python] How to use Typetalk API
[Road to Python Intermediate] Call a class instance like a function with __call__
[Road to Python intermediate] Dynamically specify execution method by variable name
Basic grammar of Python3 system (how to use functions, closures, lambda functions)
[Python] Summary of how to use pandas
Use print in a Python2 lambda expression
[Python] Use pandas to extract △△ that maximizes ○○
How to install and use pandas_datareader [Python]
[Python] Road to snake charmer (1) Environment construction
[python] How to use __command__, function explanation
I want to use jar from python
[Introduction to Python] Let's use foreach with Python
[Python] How to use import sys sys.argv
Easy way to use Wikipedia in Python