The map/reduce/filter/sorted of Python basics

Source: Internet
Author: User
Tags iterable

---map (fun,iterable)

First take a look at the map () function, the map function accepts two parameters, the first parameter is the name of the function, and the second parameter is an iterative object. That is, map (fun,iterable)

The map function calculates the specific value based on the algorithm and saves the result as an iterator. We know that iterators are ' lazy ' and only output one value at a time by invoking the next function.

Take a look at one of the simplest map () examples.

#给定一组数1, 2,3 asks for the value of each number plus one after >>> def add (x):
... x+=1
... return x
...
>>> l=[1,2,3]
>>> R=map (add,l)
>>> Print (R)
<map Object at 0x03e4f510>
>>> for I in R:
... print (i)
...
2
3
4

Use map to assign multiple values to a row

X,y,z=map (int,input (' Please input your number: '). Split ()) print (x, y, z)

How do I capitalize the first letter of a name using map ()?

def daxie (name):    return name[0].upper+name[1:]def daxie2 (name):    return '%s%s '% (Name[0].upper (), name[1:]) a= [' Linghuchong ', ' Dongfangbubai ']r=map (daxie2,a) for I in R:    print (i)

--reduce

Reduce, like the map function, accepts two parameters, but the reduce function calculates the result of the current value and the result of the next value in a cumulative way.

The reduce () function first runs the first item of the iterated object as the first argument, and the second entry as the second argument into the function.

The second run will return the result of the first run of the function as the first parameter, iterating over the third item of the object as the second parameter passed into the function ...

i.e.: Reduce (f,[1,2,3,4]) =f (f (3), 4)

Take a look at a simple example of the reduce function

#from functools Import Reducedef Leijia (x, y):    #注意, the function we define must accept two parameters, otherwise it will error    return X+yprint (Reduce (leijia,[ 1,2,3,4,5]) #结果15 # Of course you can add the default parameter from Functools import reducedef Leijia (x,y,z=2):    return X+y+zprint (Reduce (leijia,[ 1,2,3,4,5]) #结果: 23

--filter

Same as two functions, accept two parameters, the first argument is the function name, the second argument is a sequence. However, the function returns the result (True/false) to determine whether the element is persisted, depending on the elements in the sequence that are acting on the function.

Consider a simple example of how to filter even numbers:

def select (num):    if num%2==0:        return True    else:        return Falser=filter (select,[1,2,3,4,5,6]) for I in R:    Print (i)

--sorted (List,key=none,reverse=false)

Sorted as the name implies, is the meaning of sorting.

>>> sorted ([22,33,55,11,44]) [11, 22, 33, 44, 55]

Sorted can also accept a key function to implement a custom sort.

1 The function specified by key will act on each element of the list. That is, each element of the list is passed into the key function as a parameter

The 2 sorted () function sorts the results returned by the keys function and returns the corresponding elements of the list according to the corresponding relationship

>>> sorted ([22,33,-11,44,-55],key=abs) [-11, 22, 33, 44,-55]

Come up with a more complicated example:

  

#  Sort by name >>> d=[(' Linghuchong ', ' Xixingdafa '), (' Dongfangbubai ', ' Kuihuabaodian '), (' Zhangwuji ', ' Qiankundanuoyi ')]>>> def By_name (a): ...     Return a[0] ...     >>> r=sorted (d,key=by_name) >>> print (R) [(' Dongfangbubai ', ' Kuihuabaodian '), (' Linghuchong ', ' Xixingdafa '), (' Zhangwuji ', ' Qiankundanuoyi ')]#  sorted by work Method >>> def BY_GONGFA (a): ...     Return a[1] ...     >>> r2=sorted (D,KEY=BY_GONGFA) >>> print (R2) [(' Dongfangbubai ', ' Kuihuabaodian '), (' Zhangwuji ', ' Qiankundanuoyi '), (' Linghuchong ', ' Xixingdafa ')]

The map/reduce/filter/sorted of Python basics

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.