English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Tutoriel de base Python

Contrôle de flux Python

Fonctions en Python

Types de données en Python

Opérations sur les fichiers Python

Objets et classes Python

Dates et heures Python

Connaissances avancées Python

Manuel de référence Python

Fonctions anonymes (Lambda) en Python

Dans cet article, vous découvrirez les fonctions anonymes, également appelées fonctions lambda. Grâce à des exemples, vous comprendrez ce qu'elles sont, leur syntaxe et comment les utiliser.

Qu'est-ce qu'une fonction lambda en Python ?

En Python, les fonctions anonymes n'ont pas de nom défini.Fonction.

Bien que 'def' utilise le mot-clé pour définir des fonctions normales en Python, le mot-clé 'lambda' est utilisé pour définir des fonctions anonymes.

Par conséquent, les fonctions anonymes sont également appelées fonctions lambda.

Comment utiliser les fonctions lambda en Python ?

Les fonctions lambda en Python ont la syntaxe suivante.

Syntaxe des fonctions lambda en Python

lambda arguments: expression

Une fonction lambda peut avoir un nombre quelconque de paramètres, mais ne peut en avoir qu'un seul expression. L'expression est évaluée et retournée. La fonction lambda peut être utilisée partout où un objet fonction est nécessaire.

Exemple de fonction lambda en Python

这是一个使输入值翻倍的lambda函数示例。

# 程序展示lambda函数的使用
is the evaluation and return expression. * 2
This is an example of a lambda function that doubles the input value.5# Program demonstrates the use of lambda functions

Output result

10

print(double( * 2)) * 2In the above program, lambda x: x

is a lambda function. Here x is the parameter, x

is the evaluation and return expression. * 2

This function has no name. It returns a function object, which is assigned to the identifier double. Now we can call it a normal function. Below is the declaration

double = lambda x: x
   Equivalent to: * 2

def double(x):

return x

Using Lambda functions in pythonWhen we temporarily need an anonymous function, we use lambda functions.In Python, we usually use it as an argument of higher-order functions (functions that take other functions as

arguments

)。Lambda functions can be used with filter(),map() and other built-in functions.

Example of using lambda with filter()

The filter() function in Python takes a function and a list as parameters.

This is an example of using the filter() function to filter out only even numbers from the list.
my_list = [1, 5, 4, 6, 8, 11, 3, 12]
# Program filters even numbers from the list2 new_list = list(filter(lambda x: (x%
print(new_list)

Output result

[4, 6, 8, 12]

== 0) , my_list))

Example of using lambda with map()

The map() function in Python takes a function and a list.

This is an example of using the map() function to double all items in the list.

# Use map() to double each item in the list
my_list = [1, 5, 4, 6, 8, 11, 3, 12]
new_list = list(map(lambda x: x * 2 , my_list))
print(new_list)

Output result

[2, 10, 8, 12, 16, 22, 6, 24]