Python three built-in functions for list filter (), map (), reduce ()

Source: Internet
Author: User
Tags arithmetic

‘‘‘
Python--version:P Ython 2.7.11
Quote:https://docs.python.org/2/tutorial/datastructures.html#more-on-lists
Add by Camel97 2017-04
‘‘‘
1.filter ()
# filter (function, sequence) Returns a sequence consisting of those items from the sequence for which < Span class= "pre" >function (item) is True. If sequence is a str , unicode or tuple , The result would be of the same type; otherwise, It is always a list . For example, to compute a sequence of numbers divisible by 3 or 5:
#你可以把 filter is used as a filter to select value that satisfies a specific condition in the original List. It has two Parameters. The first parameter is a function that returns a bool type, and the second argument can be a List. Filter () Returns a new list with the values of this new list satisfying the two conditions. first, they belong to the value of the list passed to filter (). second, the function returns True when they are passed as arguments to the function functions.
1 def F (x): 2     return or x% 5 = = 03Print filter (f, range (2,))45 = = >[3, 5, 6, 9, 10, 12, 15, 18, 20, 21, 24]

obviously, Filter () filters out the number of the original list (range (2,25) that can be divisible by 3 or divisible by 5.

2.map ()

# calls for each of the map(function, sequence) function(item) sequence ' s items and returns a list of the return Values. For example, to compute some cubes:

The #map function can pass each value in the list to a function and combine the results returned by each function together to create a new list

#它可以被用来这样操作: first define a function that performs an arithmetic operation on one parameter, and then use the Map function to define an arithmetic operation for a function of each value in the list

1 Print map (Lambda x:x*x*x, range (1, one))23 ==>[1, 8, 27, 64, 125, 216, 343 , 512, 729, 1000]

This demo is used to perform an f (x) = x*x*x operation on each value in the list (range 1,11)

#More than one sequence may be passed; The function must then has as many arguments as there is sequences and was called with the corresponding item from each s Equence (or None If some sequence is shorter than another).

#map function can pass more than one List. however, The function must also perform arithmetic operations on more than one parameter. The number of lists and the number of arguments to the function should be corresponding.

1 seq = range (8)2print map (Lambda x,y:x+y, seq, seq)3 C8>4 ==>[0, 2, 4, 6, 8, 10, 12, 14]

This demo shows how to sum the corresponding value in the two list

1 seq = range (8)2 seqcopy = range (7)3print map (lambda x , y:x+y, seq, seqcopy)45for'int'and  'nonetype'

, the program will report a TypeError error when the number of value in the two list is not equal. Because a nonetype cannot be operated with any type of data

3.reduce ()

# reduce(function, sequence) Returns a single value constructed by calling the binary function function on the first and the S equence, then on the result and the next item, and so On. For example, to compute the sum of the numbers 1 through 10:

The #reduce () function returns a value instead of a List. First you need to define a function that requires an arithmetic operation on two Parameters. The reduce () function first does this arithmetic for the first value and the second value in the list, which gets a result. It then obtains a new result for this arithmetic operation on the result and the third value in the List. It goes on like this until all of the value in the list participates in the operation and returns the last Result.

1 print reduce (lambda x,y:x+y, range)23 ==>55

Summed operation for 10 numbers in range (11) completed

#If there ' s only one item in the sequence, its value is returned; If the sequence is empty, an exception is Raised.

#如果 There is only one data in the list, then reduce () will not perform any operations but return the number directly. If the list is empty, reduce () will report an exception TypeError

1 print reduce (lambda x,y:x+y,[1])2 ==>134print Reduce (lambda x,y:x+y,[])5 ==>typeerror:reduce () of empty sequence with no initial valu E

#A third argument can is passed to indicate the starting Value. In this case the starting value was returned for an empty sequence, and the function was first applied to the starting value And the first sequence item, then to the result and the next item, and so On.

You can add a third parameter #reduce (). This parameter is equivalent to an initial Value. When the third argument is given, reduce () returns the initial value for an empty list instead of throwing an exception (compared to the previous case). When the third argument is given, reduce () calculates the initial value and the arithmetic result of the first value first, and the remaining steps are the same

1 print reduce (lambda x,y:x+y, range (one), one)2 ==>6634  Print reduce (lambda x,y:x+y,[1], 2)5 ==>367Print reduce (lambda x,y:x+y, [], 0)8 ==>0

Note that although we say that these three functions are very useful in the list, they are not used only in the list data type.

Of course. The incoming data type needs to be able to be iterated.

Python three built-in functions for list filter (), map (), reduce ()

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.