Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are lambda, map and filter of Python knowledge points?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces what is the lambda and map and filter of Python knowledge points. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

This paper introduces the use of lambda,map,filter function in Python through an example.

Lambda

The lambda operator (or lambda function) is usually used to create small, one-time anonymous function objects. Its basic syntax is as follows:

Lambda arguments: expression

The lambda operator can have any number of arguments, but it can only have one expression and cannot contain any statements, returning a function object that can be assigned to any variable.

Let's understand it through an example. First, take a look at a Python function:

Def add (x, y): return Xeroy # call the functionadd (1,2) # Output: 3

The above function, named add, takes two arguments x and y and returns their sum.

Next, let's change the above function into a lambda function:

List_a = [1,2,3,4,5] filter_obj = filter (lambda x: X% 2 = = 0, list_a) # filter object even_num = list (filter_obj) # Converts the filer obj to a listprint (even_num) # Output: [2,4]

In lambda x, y: xroomy, x and y are the parameters of the function, and xroomy is the expression, which is executed and returns the result.

Lambda x, y: X + y returns a function object that can be assigned to any variable. In this case, the function object is assigned to the add variable. If we look at add's type, we can see that it is a function

Type (add) # Output: function

Most lambda functions are passed as an argument to a function that requires a function object as a parameter, such as map,reduce,filter and so on.

Map

The basic syntax of map is as follows:

Map (function_object, iterable1, iterable2,...)

The map function requires a function object and any number of iterables, such as list,dictionary, and so on. It performs a function_object for each element in the sequence and returns a list of elements modified by the function object.

Examples are as follows:

Def add2 (x): return x+2map (add2, [1Jing 2Jing 3Jue 4]) # Output: [3Med 4Jing 5Jue 6]

In the above example, map executes the add2 function on each element in list, 1, 2, 3, 4, and returns [3, 4, 5, 4, 6]

Then take a look at how to rewrite the above code with map and lambda:

Map (lambda x: Xerox 2, [1 Output 2, 3 and 4]) # Output: [3, 4, 4, 5 and 6]

It can be done in just one line!

Iterate through dictionary using map and lambda:

Dict_a = [{'name':' python', 'points': 10}, {' name': 'java',' points': 8}] map (lambda x: X ['name'], dict_a) # Output: [' python', 'java'] map (lambda x: X [' points'] * 10, dict_a) # Output: [100,80] map (lambda x: X ['name'] = = "python", dict_a) # Output: [True False]

In the above code, each dict in dict_a is passed to the lambda function as an argument. The lambda function expression acts on the result of each dict as output.

The map function acts on multiple iterables

List_a = [1,2,3] list_b = [10,20,30] map (lambda x, y: X + y, list_a, list_b) # Output: [11,22,33]

Here, the I element of list_a and list_b is passed as an argument to the lambda function.

In Python3, the map function returns an iterator (iterator) or map object for lazy computation (lazily evaluated). Just as the zip function is calculated lazily.

We cannot access the element of a map object through index, nor can we use len () to get its length.

However, we can cast the map object to list:

Map_output = map (lambda x: Xerox 2, [1,2,3,4]) print (map_output) # Output: map object: list_map_output = list (map_output) print (list_map_output) # Output: [2,4,6,8]

Filter

The basic syntax of filter is as follows:

Filter (function_object, iterable)

The filter function takes two arguments, and function_object returns a Boolean value (boolean). Calling function_object,filter on each element of iterable returns only those elements that satisfy the function_object value of True.

Like the map function, the filter function returns a list, but unlike the map function, the filter function can only have one iterable as input.

Example:

Returns an even number:

A = [1,2,3,4,5,6] filter (lambda x: X% 2 = = 0, a) # Output: [2,4,6]

Filter the list of dicts:

Dict_a = [{'name':' python', 'points': 10}, {' name': 'java',' points': 8}] filter (lambda x: X ['name'] = =' python', dict_a) # Output: [{'name':' python', 'points': 10}]

Like map, the filter function returns a lazy computed filter object or iterator in Python3. We cannot access the element of a filter object through index, nor can we use len () to get its length.

List_a = [1,2,3,4,5] filter_obj = filter (lambda x: X% 2 = = 0, list_a) # filter object even_num = list (filter_obj) # Converts the filer obj to a listprint (even_num) # Output: [2,4] the lambda and map and filter of Python knowledge points are shared here. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report