python中的Lambda表达式/函数

Explanation:

说明:

In python, there is a function named Lambda. Lambda function is an anonymous function - that means the function which does not have any name.

在python中,有一个名为Lambda的函数。 Lambda函数是一个匿名函数-表示该函数没有任何名称。

When we declare a function, we use "def" keyword to define a function with a suitable function name. But lambda function does not require that.

当我们声明一个函数时,我们使用“ def”关键字来定义一个具有合适函数名的函数。 但是lambda函数不需要。

Syntax to declare a lambda expression/function:

声明lambda表达式/函数的语法:

    lambda parameterlist : expression

Where,

哪里,

  • lambda is a reserved word that defines a lambda expression.

    lambda是定义lambda表达式的保留字。

  • parameterlist is a comma-separated list of parameters as you would find in the function definition (but notice the lack of parentheses).

    参数列表是用逗号分隔的参数列表,您可以在函数定义中找到(但请注意缺少括号)。

  • expression is a single Python expression. The expression cannot be a complete statement.

    expression是单个Python表达式。 该表达式不能是完整的语句。

Note:

注意:

  • This function can take as many arguments as it needs but the expression must be single.

    该函数可以根据需要使用任意数量的参数,但表达式必须为单个。

  • You are free to use the lambda function wherever you want to use.

    您可以随时随地使用lambda函数。

Example 1: Elaborating about the difference between the Simple function and the Lambda function.

示例1:详细说明简单函数和Lambda函数之间的区别。

# simple approach we use to define the area of rectangle:
# Python code to illustrate are of rectangle   
# showing difference between def() and lambda(). 
def area(l,b): 
    return l*b; 
  
g = lambda l,b: l*b 
print('lambda function:',g(7,4)) 
  
#calling the function
print('Via Simple function:',area(7,4)) 

Output

输出量

lambda function: 28
Via Simple function: 28

Explanation of the code:

代码说明:

Here, both of the functions return the same area of a rectangle, But while using the def keyword we need to do all the function of the function and also return it. But same in lambda we just need to give the arguments and the expression which returns the answer accordingly. As it does not include any return statement. We can also put a lambda definition anywhere a function is expected, and we don’t have to assign it to a variable at all. This is the simplicity of lambda functions.

在这里,两个函数都返回矩形的相同区域,但是在使用def关键字时,我们需要完成函数的所有功能并返回它。 但是在lambda中,我们只需要提供参数和相应的表达式即可返回答案。 由于它不包含任何return语句。 我们还可以将lambda定义放在需要函数的任何地方,而我们根本不必将其分配给变量。 这就是lambda函数的简单

Example2: How we can use lambda function different forms?

例2:我们如何使用lambda函数使用不同的形式?

print('Ways to use and declare lambda functions:')
# simply defining lambda function 
#example - 1
g=lambda x, y: 3*x + y
print('Ex-1:',g(10,2))

#example - 2
f=lambda x, y: print('Ex-2:',x, y)
f(10,2)

#example - 3
h=lambda x, y: 10 if x == y else 2
print('Ex-3:',h(5,5))

#example - 4
i=lambda x, y: 10 if x == y else 2
print('Ex-4:',i(5,3))


Output

输出量

Ways to use and declare lambda functions:
Ex-1: 32
Ex-2: 10 2
Ex-3: 10
Ex-4: 2

具有filter(),map(),reduce()的Lambda函数 (Lambda functions with filter() , map() , reduce())

lambda() function can be used with the other functions like filter() , map() etc.

lambda()函数可与其他函数(例如filter() , map()等)一起使用 。

filter(): It takes the list of arguments. This function filters out all the elements in the given list which return True for the function.

filter():获取参数列表。 该函数过滤掉给定列表中所有返回该函数True的元素。

map(): map() function in python used to map all the elements of the list with its condition by the function or by the lambda function.

map(): python中的map()函数,用于通过函数或lambda函数映射列表中的所有元素及其条件。

Syntax:

句法:

    map(function_object, iterable1, iterable2,...)

Example 3: Lambda Function using the filter() , map() , reduce()

示例3:使用filter(),map(),reduce()的Lambda函数

#Using the filter() , map() with lambda() function.


# Python code to illustrate 
# filter() with lambda() 
li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61] 
li = list(filter(lambda x: (x%2 == 0) , li)) 
print('By using filter :',li)

# Python code to illustrate  
# map() with lambda()  
# to get double of a list.
l=[{'name':'includehelp', 'star':10},{'name':'yash', 'star':8},{'name':'sanjeev', 'star':8}]
for output1  in (map(lambda x : x['name'],l)):
    print('maping name:',output1)
for output2 in (map(lambda x : x['star']*10,l)):
    print('maping star:',output2)

Output

输出量

By using filter : [22, 54, 62]
maping name: includehelp
maping name: yash
maping name: sanjeev
maping star: 100
maping star: 80
maping star: 80


翻译自: https://www.includehelp.com/python/lambda-expression-function.aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值