Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the simple use of python lambda expression

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report