Introduction to Python's lambda, filter, reduce, and map

Source: Internet
Author: User

Lambda operation

Lambda operation--some people like it, some people hate it, and a lot of people are afraid. When you have finished the introduction of our chapter, we are confident that you will like it. Otherwise, you can learn Guido van Rossums prefers to use the "list comprehensions" (Recursive construction list), because he also doesn't like Lambda, map filter and reduce.

A lambda operation or lambda function is a way to create a small anonymous function, that is, the function does not have a function name. These functions are throwing (throw-away) functions, i.e. they are only created where we need them, and the lambda function is used for most of the time with the filter (), map (), and reduce () functions. Lambda attributes are added to Python because of the strong demands of Lisp programmers.

The usual syntax for a lambda function is very simple:

Lambda argument_list:expression

The argument list (argument_list) is a comma-delimited list of arguments, and the expression above is an arithmetic expression that can use these parameters. You can give the lambda function a name by assigning the function to a variable.

The following example of the lambda function returns the and of the two arguments:

Lambda x, y:x + y>>> F (2)

Map () function

When used in combination with the map function, the benefits of lambda operations can be reflected.

Map () is a function with two parameters:

R = Map (func, seq)

The first parameter, func, is a function name, and the second seq is a sequence (for example, list). Map () will apply this function to each element of the sequence seq, and then return a list of elements that have been changed by this function func

def Fahrenheit (T):     return ((Float (9)/5) *t + +) def Celsius (T):     return (Float (5)/9) * (T-32= (36.5, PNS, 37.5,39=Map (Celsius, F)

In the above example, we do not use lambda, and if we use lambda, we do not need to define and name the Fahrenheit () and Celsius () functions. You can see from the following interactive process:

>>> Celsius = [39.2, 36.5, 37.3, 37.8]>>> Fahrenheit = map (lambda x: (Float (9)/5) *x + Celsius)print  fahrenheit[102.56, 97.700000000000003, 99.140000000000001, 100.03999999999999]>>> C = map (lambda x: (Float (5)/9) * (x-32), Fahrenheit)  Print  c[39.200000000000003, 36.5, 37.300000000000004, 37.799999999999997]>>>

Map () can apply multiple lists, but these lists must have the same length. Map () will apply its lambda function on the elements of these lists, such as: first applied on the element labeled 0, and then applied on the element with subscript 1 until it reaches the subscript N:

>>> a = [1,2,3,4]>>> b = [17,12,11,10]>>> c = [ -1,-4,5,9]>> > Map (lambda x,y:x+y, A, b) [+, +, +]>>> map (Lambda x,y,z:x+y+ Z, a,b,c) [19, 18, 9, 5] >>> map (Lambda x,y,z:x+y-z, a,b,c)

We can see from the above example that the value of the parameter x is from the A list, and the value of Y from the list b,z is from list C.

Filtering

The filter (func, list) function provides an elegant way to filter out elements of function func that return a value of true.

Filter (F, l) requires a function f as its first argument. F Returns a Boolean value, such as: TRUE or false. This function will be applied to each element of the list L. Only when F returns true will the elements in this list be included in the result list.

>>> fib = [0,1,1,2,3,5,8,13,21,34,55]>>> result = Filter (lambda x:x% 2, fib) print  result[1, 1, 3, 5,,, 2,]>>> result = Filter (lambda x:x% ==< c7> 0, fib)print2, 8,[+]

Reducing a List

The function reduce (func, SEQ) continues to apply the function func on the sequence. It will return a value.

A sequence of seq = [S1, S2, S3, ..., SN], calls reduce (func, seq) to work like this:

    • First, the first and second elements of the sequence will be applied to the function func, such as Func (S1,S2), after which the list looks like this: [Func (S1, S2), S3, ..., SN]
    • Next, the function Func will be applied on the third element of the previous result and sequence, such as Func (func (S1, S2), S3), and this list now looks like: [Func (Func (S1, S2), S3), ..., SN]
    • This continues until only one element is left, and then returns the element as a result of reduce

We use the following example to illustrate this process:

>>> reduce (lambda x,y:x+y, [47,11,42,13])113

The following diagram shows the intermediate calculation steps:

Reduce () example

Use reduce to find the largest value in a column of numbers

Lambda if Else b>>> reduce (f, [47,11,42,102,13])

Calculates the number 1 through 100 and:

>>> reduce (lambda x, Y:x+y, Range (1,101))5050

Original (English): http://www.python-course.eu/lambda.php

Translation (Chinese): http://www.cnblogs.com/rhjiang/

Reprint please indicate the source.

Introduction to Python's lambda, filter, reduce, and map

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.