Home >Backend Development >Python Tutorial >Quickly learn lambda function skills in Python
Quickly learn the application skills of lambda function in Python, you need specific code examples
In the Python programming language, the lambda function is an anonymous function based on expressions. It can simplify the coding process and improve the readability and maintainability of the code. This article will introduce the basic usage of the lambda function and demonstrate its application skills in actual development through specific code examples.
The basic syntax of the lambda function is: lambda parameter list: expression
Among them, the parameter list represents the input parameters of the function, and the expression A formula is an expression that operates on parameters to obtain a result. A lambda function can accept any number of parameters but can only return a result.
The following is a simple lambda function example that calculates the sum of two numbers:
sum = lambda x, y: x y
print(sum(3, 5)) # Output 8
The lambda function is often used in list sorting, and the key parameter is used to specify the basis for sorting. Here is an example to sort a set of numbers in ascending order:
nums = [1, 5, 3, 2, 4]
sorted_nums = sorted(nums, key=lambda x: x)
print(sorted_nums) # Output [1, 2, 3, 4, 5]
lambda function can also be used to filter the list, only Keep elements that meet the condition. Here is an example to filter out numbers greater than 5:
nums = [1, 5, 3, 8, 2, 6]
filtered_nums = list(filter(lambda x: x print(filtered_nums) # Output [1, 5, 3, 2]
lambda function can be used to map the list Perform a mapping operation on each element to obtain a new list. Here is an example of multiplying a set of numbers by 2:
nums = [1, 5, 3, 2, 4]
mapped_nums = list(map(lambda x: x * 2, nums ))
print(mapped_nums) # Output [2, 10, 6, 4, 8]
A powerful feature of lambda function The reason is that it can be passed as a parameter to other functions. Here is an example of using a custom sort_by function to sort a set of strings by length:
def sort_by_length(strings):
return sorted(strings, key=lambda x: len(x))
strings = ["apple", "banana" , "cherry", "date"]
sorted_strings = sort_by_length(strings)
print(sorted_strings) # Output ['date', 'apple', 'cherry', 'banana']
lambda function can be very flexible, including basic arithmetic operations, conditional judgments, complex logic, etc. The following is an example that divides a set of numbers into two lists of positive numbers and negative numbers based on conditional judgment:
nums = [-1, 2, -3, 4, -5]
positive_nums = list( filter(lambda x: x > 0, nums))
negative_nums = list(filter(lambda x: x print(positive_nums) # Output [2, 4]
print(negative_nums) # Output [-1, -3, -5]
Summary:
Through the above specific code examples, we understand the application skills of lambda functions in Python. Lambda functions can simplify code writing and are especially suitable for simple functions such as sorting, filtering, and mapping. At the same time, the lambda function can also be passed as a parameter to other functions, and it can also play an important role in some complex programming scenarios.
It should be noted that although the lambda function is convenient, it also needs to be used with caution. For more complex logic and operations, it is recommended to use conventional named functions to improve code readability and maintainability.
The above is the detailed content of Quickly learn lambda function skills in Python. For more information, please follow other related articles on the PHP Chinese website!