Python function filter () map () reduce ()

Source: Internet
Author: User

1.filter (BOOL_FUNC,SEQ)

Filter () is a ' filtering function ', and also receives a function and a sequence, filter () acts on each element of the sequence by the function of the descendant, and then decides whether to persist or discard the element based on whether the return value is TRUE or False

Example:

  1. def fr (x):

  2. return x%2==1

  3. print   ' Filter1: ' , Filter (Fr,range (1,51)) #筛选出100以内的所有奇数   

  4. Print ' filter2: ', Filter (fr,[1,2,3,4])

Output:

Filter1: [1, 3, 5, 7, 9, one, ,, +,,, at, +, (+), (+), (+), ()
Filter2: [1, 3]

filter Built-in functions Python implementation:

  1. >>> def filter (BOOL_FUNC,SEQ):

  2. FILTERED_SEQ = []

  3. for Eachitem in seq:

  4. if bool_func (eachitem):

  5. Filtered_seq.append (Eachitem)

  6. return Filtered_seq


2.map (func,seq1[,seq2 ...])

map (): the function func acts on each element of a given sequence and provides the return value with a list, and if Func is none,func as an identity function, returns a list of n tuples containing the set of elements in each sequence.

  1. >>> map (Lambda x:none,[1,2,3,4])

  2. [None, None, none, none]

  3. >>> Map (Lambda x:x * 2,[1,2,3,4])

  4. [2, 4, 6, 8]

  5. >>> Map (Lambda x:x * 2,[1,2,3,4,[5,6,7])

  6. [2, 4, 6, 8, [5, 6, 7, 5, 6, 7]]

  7. >>> map (Lambda x:none,[1,2,3,4])

  8. [None, None, none, none]

The python implementation of the map built-in function:


  1. >>> def map (FUNC,SEQ):

  2. MAPPED_SEQ = []

  3. for Eachitem in seq:

  4. Mapped_seq.append (func (Eachitem))

  5. return Mapped_seq


3.Reduce (func,seq[,init])

reduce (): Func is a two-tuple function that functions func on the elements of a SEQ sequence, each carrying a pair (the previous result and the element of the next sequence), continuously acting on the resulting subsequent result with the existing result and the next value, Finally, we reduce our sequence to a single return value: If Init is given, the first comparison will be init and the first sequence element instead of the head two elements of the sequence.

  1. >>> Reduce (lambda x,y:x + y,[1,2,3,4])

  2. 10

  3. >>> Reduce (lambda x,y:x + y,[1,2,3,4],10)

  4. 20

The python implementation of reduce:

  1. >>> def Reduce (bin_func,seq,initial=none):

  2. LSEQ = list (seq)

  3. if initial is None:

  4. res = lseq.pop (0)

  5. Else:

  6. res = Initial

  7. for Eachitem in Lseq:

  8. res = Bin_func (res,eachitem)

  9. return Res




This article is from the "Operations Diary" blog, make sure to keep this source http://gyyblog.blog.51cto.com/8666992/1921278

Python function filter () map () reduce ()

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.