Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the simple use of python lambda expression

Shulou Source: shulou.com Published: 2022-06-01 18:24:17 09月29日 Update

What is the simple use of python lambda expression? for this question, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

Simple usage of python lambda expression

When learning conditional operations, you can use ternary operations to express simple if else statements, that is:

# ordinary function # #

# define function (normal way)

Def func (arg):

Return arg + 1

# execute function

Result = func (123)

# lambda # #

# define function (lambda expression)

My_lambda = lambda arg: arg + 1

# execute function

Result = my_lambda (123)

The meaning of the existence of lambda is the concise representation of simple functions.

Built-in function two

1. Map

Traverse the sequence, operate on each element in the sequence, and finally get a new sequence.

Li = [11,22,33] new_list = map (lambda a: a + 100, li)

Li = [11,22,33] sl = [1,2,3] new_list = map (lambda a, b: a + b, li, sl)

II. Filter

Filter the elements in the sequence, and finally get the qualified sequence.

Li = [11,22,33] new_list = filter (lambda arg: arg > 22, li) # filter the first parameter is empty, which will get the original sequence

III. Reduce

Accumulate all elements in the sequence

Li = [11,22,33] result = reduce (lambda arg1, arg2: arg1 + arg2, li) # the first parameter of reduce, the function must have two parameters # the second parameter of reduce, the third parameter of the sequence # reduce to be looped, and the answer to the question about the simple use of python lambda expression is shared here. I hope the above content can be helpful to everyone, if you still have a lot of doubts unsolved. You can follow the industry information channel for more related knowledge.

Tags: Functions sequences parameters expressions elements questions ordinary more conditions help solutions operations easy concise easy three two content boys buddies Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Linux NVidia Microsoft macOS