程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Learn Lambda Functions in Python

編輯:Python

Lambda表達式是一種在PythonA quick way to create anonymous functions in .An anonymous function is like a one-time-use function,沒有名字.You only need to use it once,Then move on in the program.Lambda 函數通常與 map() 和[filter()]結合使用.lambdaMethods of functions are a convenient way to quickly define a function that can be used with these functions in a shorthand form.在本教程中,We will introduce a few examples,說明如何在 Python 中定義 lambda 函數,and how to use them effectively.


from function toLambda

一個LambdaFunctions are just plainPythonA shorthand version of the function.So to better understand how inPython中制作一個lambda函數,We can turn an ordinary function into one step by steplambda函數.首先,Let's look at a standard Python 函數的簡單例子.

def plus_one(num):
result = num + 1
return result
復制代碼
plus_one(7)
復制代碼
8
復制代碼

去掉了 result變量

We can simply return the result of this calculation,而不是取num變量,加一,Then store in the result.

def plus_one(num):
return num + 1
復制代碼
plus_one(7)
復制代碼
8
復制代碼

Make the function a one-liner

Now we can simplify the function even further by doing the calculation in one line.

def plus_one(num): return num + 1
復制代碼
plus_one(7)
復制代碼
8
復制代碼

刪除 def關鍵字

在這裡,我們把defKeyword and the function we assign to us**(def plus_one()**)is removed along with the parentheses.

num: return num + 1
復制代碼

刪除 return關鍵字

Lambda functions do not返回語句,Because any lambda function would imply返回語句.

num: num + 1
復制代碼

添加lambda關鍵詞

最後,We can prepend the stripped expressionlambda關鍵字,瞧!這就是一個lambda函數.我們就有了一個lambda函數.

lambda num: num + 1
復制代碼

將Lambda分配給一個變量

為了利用lambda函數,You will often combine it with other functions egmap()或filter()結合起來使用.It is also commonly associated with reduce()函數一起使用.We'll look at this later.然而,你使用lambdaThe first way is to simply assign it to a variable,Then use that variable as a function.Let's see how this is done.

plus_one = lambda num: num + 1
復制代碼
plus_one(7)
復制代碼
8
復制代碼

使用一個 lambda函數與 map()

map()The first argument to a function is always a function itself.它被稱為轉換函數.下面是一個使用map()的lambda的例子.

nums = [1, 2, 3, 4, 5]
result = map(lambda x: x * 2, nums)
print(list(result))
復制代碼
[2, 4, 6, 8, 10]
復制代碼

使用一個 lambda函數與 filter()

filter()The function creates a list of elements for which the function returns true.它經常與lambda表達式一起使用.這裡有一個例子,We just want greater than3的數字.

nums = [1, 2, 3, 4, 5]
result = filter(lambda x: x > 3, nums)
print(list(result))
復制代碼
[4, 5]
復制代碼

使用一個 lambda函數與 reduce()

reduce()The function performs rolling calculations on consecutive pairs of values ​​in a list.A common example is summing all values ​​in a list,So let's try it on our simple list of numbers.

from functools import reduce
nums = [1, 2, 3, 4, 5]
result = reduce(lambda x, y: x + y, nums)
print(result)
復制代碼
15
復制代碼

if語句與lambda

你能用 lambda 函數使用 Python 的if語句嗎?Why yes,你可以.讓我們看一個例子.

result = map(lambda str: str.capitalize() if 'a' in str else str, 'abracadabra')
print(list(result))
復制代碼
['A', 'b', 'r', 'A', 'c', 'A', 'd', 'A', 'b', 'r', 'A']
復制代碼

Python Lambda 函數總結

  • Lambdas is a one-line function.
  • 也被稱為匿名函數.
  • Usually used when you don't want to use a function twice in your program.
  • It works like a normal function,Even behave like them.
  • lambdaThe body is a single expression,而不是一個語句塊.
  • Quickly make temporary functions,without having to define a function correctly,使用 def.
  • lambdais designed for coding simple functions.
  • 正式的defFunctions should be used for larger tasks.

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved