Python notes (17) - Advanced Functions

For understanding the function

min for the minimum function can be;

In [1]: min(1,3,55,4)
Out[1]: 1

 

The min function assigned to the variable a;

In [2]: a=min

In [3]: a
Out[3]: <function min>

 

View a, as a function of its type, we can see is actually a function of variables

Built-in higher-order functions

map function

All of a set of data elements within a sequence to make the operation function definition

[root@centos01 python]# cat map_test.py 
#!/usr/bin/env python
# coding:utf-8
def fun(x):
    return x**2

print map(fun,[1,2,3,4])
[root@centos01 python]# python map_test.py 
[1, 4, 9, 16]

 

reduce function

It must be two variables, a set of elements one by one to do the discharge operation back function defined

[@ centos01 the root Python] # CAT reduce_test.py 
# Coding: UTF. 8- 
# Description: 10 factorial 
DEF Fun (X, Y):
     return X * Y 

Print the reduce (Fun, Range (1,11 )) 
[the root centos01 Python @] # Python reduce_test.py 
3628800

filter function

A set of data elements one by one into the fun () function to determine if it is true that the output element, or does not output

[@ centos01 the root Python] # CAT filter_test.py 
# Coding: UTF. 8- 
# Description: to find all the even between 10 ~ 0 
DEF Fun (X):
     return X% 2 == 0 

Print filter (Fun, Range ( . 11 )) 
[the root @ centos01 Python] # Python filter_test.py 
[0, 2,. 4,. 6,. 8, 10]

sorted function

A group of data sorted according to the content defined in the function

Directly sorted sort, because the ASCII code table is sorted, we usually think of much the same sort results:

In [4]: sorted(["alic","Alex","bon","Hello","Wee"])
Out[4]: ['Alex', 'Hello', 'Wee', 'alic', 'bon']

 

Therefore, the first elements are converted to uppercase, during the sort, it is consistent with what we usually think of the sort:

[@ centos01 the root Python] # CAT sorted_test.py 
# Coding: UTF. 8- 
DEF Fun (X, Y): 
    S1 = x.upper ()   # string is converted to uppercase 
    S2 = y.upper ()
     return 0   # use the comparison function must have a return value, otherwise an error, so here just to return a value 

Print sorted ([ " alic " , " Alex " , " bon " , " the Hello " , " Wee " ], Fun)   # in these inside the built-order functions, sorted when in use only a function name into a data set used later 
[@ centos01 the root Python] # python sorted_test.py 
['alic', 'Alex', 'bon', 'Hello', 'Wee']

sorted key method:

You can sort the elements to a keyword included

The In [. 5]: = Goods [[ " Apple " , 2,20], [ " Book " , 5,90], [ " FISH " , 3,30]]    # a list of three there is also a list of 
In [6] : DEF min_one (X):         # function operates: returns the list of third elements 
   ...:      return X [2 ] 
   ...:  
the in [ . 7]: the sorted (Goods, Key = min_one)     # to each list which third element for sorting keywords sorted 
Out [. 7]: [[ ' Apple ' , 2, 20 is], [ ' FISH ' ,. 3, 30], [ ' Book ' ,. 5, 90 ]] 
the In [. 8]: the sorted (Goods, Key = min_one) [0] [0]     # is output from the sorting results in a first element of the first list 
Out [. 8]: ' Apple '

lambda anonymous function

For some simple functions can not write a complete functional form, it can be used instead of an anonymous function , e.g. lambda x: x ** 2 equivalent

def fun(x): 

  return x**2

[root@centos01 python]# cat lambda_test.py 
# coding:utf-8
print map(lambda x:x**2,[1,2,3,4])
print reduce(lambda x,y:x*y,range(1,11))
print filter(lambda x:x%2==0,range(11))
[root@centos01 python]# python lambda_test.py 
[1, 4, 9, 16]
3628800
[0, 2, 4, 6, 8, 10]

 

Guess you like

Origin www.cnblogs.com/vaon/p/11120820.html