I often wondered, "Should the code I'm writing now be a function, or should I write it directly in the main: thinking:", but at that time I came across a lambda expression and that was It's very easy to handle, and I was able to put it together compactly when I looked over the entire code, so I'll write an article with a memorandum. : writing_hand:
I would like to explain the lambda expression with code. Also, this time, I will deal with addition as a simple example, but I would like to explain how to write practical code in the next section.
python
def hoge(num1: int, num2: int):
num = num1 + num2
return num
Like this, even a function that does simple addition needs to add `` `return``` at the end. Also, when executing the above function,
python
print(hoge(2, 6))
# 8
In order to confirm that the function is processed correctly, I think that it will be confirmed by inserting a `print statement`
.
However, if you haven't done any complicated processing to make it a function, or if you don't want to use that function elsewhere, you may want to write it a little more compactly.
You can use a lambda expression in such a case!
The above addition is described by a lambda expression as follows.
python
print((lambda num1, num2:num1 + num2)(2, 6))
# 8
Explanation of lambda expression: (lambda argument name: argument processing) (value required for calculation) With just such a description, it was very easy to read and I was able to code it compactly: blush: It is also possible to make a lambda expression with arguments by assigning it to a variable.
python
hoge = lambda num1, num2:num1 + num2
print(hoge(2, 6))
# 8
Also, personally speaking, it's kind of cool to be able to code in a lambda expression: smile: (It looks like someone who can do it ...)
If you keep the explanation of the above item, it seems that it will be "It does not change much even if you write it with a function !: grimacing:", so I would like to think about how to demonstrate the true value of the lambda expression here. I will!
For example ...
python
hoge_list = [3, -5, 10, 6, -2, -1]
If you remove negative values from this list and want to sort them in ascending order, use a lambda expression ...
python
print(sorted(filter(lambda num_plus: num_plus >= 0, hoge_list)))
# [3, 6, 10]
With the feeling like, the first `sorted function ``` executes the process of arranging in ascending order, and the
`filter function ``` describes the condition like an if statement, so it is very easy. Can be described in! : tada:
`sorted function`
can be arranged in descending order, but this time I will omit it: bow_tone1:
Also, since it processes to the end of the element, there is no need to write a for statement, so it looks compact and easy to see!By using a lambda expression, you don't have to bother to write for statements and if statements, so you can express the code in a compact and easy-to-read manner, and it seems to be useful even if you hesitate to make it a function. , I would like to use it more and more when I encounter a situation where I can use the lambda expression! : muscle:
Recommended Posts