The Map,reduce,filter function in Python3

Source: Internet
Author: User

Printing the Map,filter function directly in Python2 will output the result directly. However, in the Python3 to make some changes, the output need to use the list () for display conversion, and the reduce function is placed in the Functools package, the following code

From functools import reduce
import math

def format_name (s): Return
    S.upper ()

def is_odd (x):
    return x 2 = 1

def sqr_integer (x):
    r = Math.floor (math.sqrt (x)) return
    x = = R*r

def f (x, y):
    return x + y
map puts function f on each element of the list, gets a iterator  and returns.
Print (Map (format_name, [' Adam ', ' LISA ', ' BarT ']))

# Reduce () incoming function f must receive two parameters, reduce () Call function f repeatedly on each element of the list and return the final result value. Reduce () can also receive the 3rd optional parameter as the initial value of the calculation.
print (Reduce (f, [1, 3, 5, 7, 9], M))

# Filter () automatically filters out elements that do not meet the criteria according to the results of the judgment, and returns the iterator that are composed of the eligible elements.
Print (Filter (is_odd, [1, 4, 6, 7, 9,,))

Print (List filter (Sqr_integer,range (100)))

The results of the operation are as follows

[' ADAM ', ' LISA ', ' BART ']
"[
1, 7, 9,"]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

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.