


Python zero-based introduction to lambda expressions and built-in functions of filter and map
1.Lambda expression
The syntax of Lambda expression is: lambda parameter: operation on the parameter
The reason why Lambda expression is introduced is mainly due to the following points:
(1) Python writing When executing scripts, using lanbda can save the process of defining functions.
(2) For some more abstract functions that only need to be called once or twice to execute the entire program, there is no need to worry about naming issues. .
(3) Simplify the readability of the code. There is no need to go to the definition part of the function to read.
Note that when using Lambda expressions, first assign the Lambda expression to a variable, and then Use this variable as if calling a function to get the result of the operation.
def ds(x): return 2*x+1print(ds(5)) #lambda表达式后面紧跟着的是参数,冒号后面 #紧跟着的是对于参数进行的操作 lambda x:2*x+1g=lambda x:2*x+1 print(g(5))def add(x,y): return (x+y) print(add(3,4)) g=lambda x,y : x+y print(g(3,4))
2.filter() function
Syntax: filter(None or Function, iterable)
The function is to select the iterable data through conditions Data with a result of 1 or true is more convenient to use in combination with lambda.
The function of the filter in the example code is to filter out odd numbers between 1 and 10
list1=list(filter(lambda x:x%2,range(0,10))) print(list1)
3.map() function
The map() function calculates the iterable data one by one according to the mapping defined by the first parameter.
list2=list(map(lambda x:x*2,range(10))) print(list2)
The basic knowledge about functions ends here. The next blog will collect relevant knowledge about data structures such as dictionaries.
The above is the content of lambda expressions and filter and map built-in functions in Part 8 of Python Zero Basic Introduction. For more related content, please pay attention to the PHP Chinese website (www.php. cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In C++, there are two ways to handle exceptions using Lambda expressions: catch the exception using a try-catch block, and handle or rethrow the exception in the catch block. Using a wrapper function of type std::function, its try_emplace method can catch exceptions in Lambda expressions.

In C++, a closure is a lambda expression that can access external variables. To create a closure, capture the outer variable in the lambda expression. Closures provide advantages such as reusability, information hiding, and delayed evaluation. They are useful in real-world situations such as event handlers, where the closure can still access the outer variables even if they are destroyed.

The advantages of lambda expressions in C++ multi-threaded programming include simplicity, flexibility, ease of parameter passing, and parallelism. Practical case: Use lambda expressions to create multi-threads and print thread IDs in different threads, demonstrating the simplicity and ease of use of this method.

C++ Lambda expressions support closures, which save function scope variables and make them accessible to functions. The syntax is [capture-list](parameters)->return-type{function-body}. capture-list defines the variables to capture. You can use [=] to capture all local variables by value, [&] to capture all local variables by reference, or [variable1, variable2,...] to capture specific variables. Lambda expressions can only access captured variables but cannot modify the original value.

There are three ways to capture lambda expressions of external variables in C++: Capture by value: Create a copy of the variable. Capture by reference: Get a variable reference. Capture by value and reference simultaneously: Allows capturing of multiple variables, either by value or by reference.

In C++, you can use Lambda expressions as function parameters to achieve the flexibility of callback functions. Specifically: Parameter passing: wrap the Lambda expression through std::function and pass it to the function in the form of a function pointer. Return value processing: Specify the return value type when declaring the callback function pointer using std::function. Practical case: Optimize callbacks in GUI event processing, avoid creating unnecessary objects or function pointers, and improve code simplicity and maintainability.

How to perform lazy evaluation using C++ lambda expressions? Use lambda expressions to create lazily evaluated function objects. Delayed computation defers execution until needed. Calculate results only when needed, improving performance.

Performance tips for optimizing C++ lambda expressions include: Avoiding unnecessary creation of lambda objects Explicitly capturing the smallest object via std::bind Moving captured variables using std::move to avoid copying Optimizing the lambda body to avoid unnecessary Memory allocation, repeated calculations, and global variable access
