Python函数式编程 map reduce filter

函数式编程,使代码简洁高效。

函数编程语言最重要的基础是λ演算(lambda calculus),函数可以像数值一样被赋值于变量,还可以作为其他函数的输入(引数)和输出(传出值)进行传递。

函数可以当做参数来进行传递,形成所谓的高阶函数,形如 z=g(f(x),y),还能像变量一样被创建和修改。

Map函数:

  map(func, *iterables),作用是将一个列表映射到另一个列表。

class map(object):
    """
    map(func, *iterables) --> map object
    
    Make an iterator that computes the function using arguments from
    each of the iterables.  Stops when the shortest iterable is exhausted.
    """
View Code

使用方法:

def f(x):
    return x**2

li = range(1,10)
res = map(f,li)
print(res)
print(list(res))

"""
<map object at 0x000000000117E2E8>
[1, 4, 9, 16, 25, 36, 49, 64, 81]
"""

       

map(functioniterable...)

map()函数接收两个参数,一个是函数,一个是可迭代的对象,map将传入的函数依次作用到序列的每个元素,返回一个map对象,不是list。

基本等价于 [f(x) for x in interable],列表推导比map效率要高一些

map(lambda x: x+1, range(1, 3)) => [x+1 for x in range(1,3)]

str = ["far","foo","bar"]
mp = map(lambda x:x.upper(),str)
res = list(mp)
print(res)

"""
['FAR', 'FOO', 'BAR']
"""
View Code

Reduce函数

  reduce(function, sequence[, initial]),对可迭代对象依次做累计操作,如依次相加或相乘。

  reduce()方法接收一个函数作为累加器(accumulator),数组中的每个值(从左到右)开始合并,最终为一个值。

def reduce(function, sequence, initial=None): # real signature unknown; restored from __doc__
    """
    reduce(function, sequence[, initial]) -> value
    
    Apply a function of two arguments cumulatively to the items of a sequence,
    from left to right, so as to reduce the sequence to a single value.
    For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
    ((((1+2)+3)+4)+5).  If initial is present, it is placed before the items
    of the sequence in the calculation, and serves as a default when the
    sequence is empty.
    """
View Code

直接使用会报错

reduce(lambda x, y : x + y, [1, 3, 5, 7, 9])
"""
NameError: name 'reduce' is not defined
"""

正确的使用是:reduce是functools中的一个函数,需要引用:from functools import reduce

使用方法:

from functools import reduce

res1 = reduce(lambda x, y: x*y, [1, 2, 3])
res2 = reduce(lambda x, y : x + y, [1, 3, 5])
print(res1)
print(res2)

"""
6
9
"""

 Python内置的all(),any(),sum(),max(),min()等函数都是从reduce()衍生而来。

Filter函数

  filter(function or None, iterable),作用是按照所定义的函数过滤掉列表中的一些元素

class filter(object):
    """
    filter(function or None, iterable) --> filter object
    
    Return an iterator yielding those items of iterable for which function(item)
    is true. If function is None, return the items that are true.
    """
View Code

使用方法:

flt = filter(lambda x: x > 5, range(10))
res = list(flt)
print(flt)
print(res)
"""
<filter object at 0x0000000000649A58>
[6, 7, 8, 9]
"""

生成器

通过列表生成式,可以直接创建一个列表。但受内存限制,列表容量有限。创建一个包含100万个元素的列表,会占用很大的存储空间,如果我们仅仅需要访问其中几个元素,那绝大多数元素占用的空间都白白浪费了。

所以,如果列表元素可以按照某种算法推算出来,那我们是否可以在循环的过程中不断推算出后续的元素呢?这样就不必创建完整的list,从而节省大量的空间。在Python中,这种一边循环一边计算的机制,称为生成器:generator。

要创建一个generator,有很多种方法。最简单的方法是,只要把一个列表生成式的[]改成(),就创建了一个generator:

lis = [x*x for x in range(10)]  # list
gen = (x*x for x in range(10))  # generator对象,与list的区别,只是最外层的()
print(lis)
print(gen)
"""
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
<generator object <genexpr> at 0x000000000118C8E0>
"""

  一个函数调用时返回一个迭代器,那这个函数就叫做生成器(generator);

  如果函数中包含yield语法,那这个函数就会变成生成器。

def cube(n):
    for i in range(n):
        yield i ** 3

for i in cube(5):
    print(i)

"""
0
1
8
27
64
"""
View Code

生成器的特点:

1)生成器只有在调用时才会生成相应的数据;

2)只记录当前位置;

3)只有一个__next__()方法;

yield 的实现

def simple_yield(start):
    n = start
    while True:
        yield n
        n += 1

if __name__ == '__main__':
    for i in simple_yield(5):
        print(i)
        if i >= 10:
            break
"""
5
6
7
8
9
10
"""
View Code

从 simple_yield 中拿数据,拿到数据后,yield 会立即返回(可能有返回值),函数本身并没有结束,只是被挂起,直到下次调用(for 循环会调用 next 方法)再从挂起的地方(yield)开始执行。

怎么打印出generator的每一个元素?

1、如果要一个一个打印出来,可以通过next()函数获得generator的下一个返回值;

2、for循环迭代

lis = [x*x for x in range(10)]  # list
gen = (x*x for x in range(10))  # generator对象,与list的区别,只是最外层的()
print(lis)
print(gen)
print(next(gen))
print(next(gen))
print(next(gen))

for i in gen:
    print(i)

"""
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
<generator object <genexpr> at 0x000000000119C8E0>
0
1
4
9
16
25
36
49
64
81
"""
View Code

斐波拉契数列(Fibonacci)

除第一个和第二个数外,任意一个数都可由前两个数相加得到:1, 2, 3, 5, 8, 13, 21, 34, ...

def fib(max):
    n,a,b = 0,0,1
    while n < max:
        print(b)
        a,b = b, a + b
        n += 1
View Code

迭代器

可以直接作用于for循环的数据类型有以下几种:

一类是集合数据类型,如listtupledictsetstr等;

一类是generator,包括生成器和带yield的generator function。

这些可以直接作用于for循环的对象统称为可迭代对象:Iterable。可以使用isinstance()判断一个对象是否是Iterable对象。

from collections import Iterable

lt1 = isinstance([],Iterable)
lt2 = isinstance({},Iterable)
lt3 = isinstance('abc',Iterable)
lt4 = isinstance((x for x in range(10)), Iterable)
lt5 = isinstance(100, Iterable)

print(lt1, lt2,lt3,lt4,lt5)
"""
True True True True False
"""
View Code

凡是可作用于for循环的对象都是Iterable类型;

凡是可作用于next()函数的对象都是Iterator类型,它们表示一个惰性计算的序列;

集合数据类型如listdictstr等是Iterable,但不是Iterator,不过可以通过iter()函数获得一个Iterator对象。

Python的for循环本质上就是通过不断调用next()函数实现的,例如:

for in [1,2,3,4,5]:

pass
 
实际上完全等价于:
# 首先获得Iterator对象:
it = iter([1, 2, 3, 4, 5])
# 循环:
while True:
    try:
        # 获得下一个值:
        x = next(it)
    except StopIteration:
        # 遇到StopIteration就退出循环
        break

装饰器

本质上是个函数,功能是装饰其他函数——就是为其他函数添加附加功能。

装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志、性能测试、事务处理等。装饰器是解决这类问题的绝佳设计,有了装饰器,我们就可以抽离出大量函数中与函数功能本身无关的雷同代码并继续重用。概括的讲,装饰器的作用就是为已经存在的对象添加额外的功能。

装饰器原则:

1)不能修改被装饰函数的源代码;

 2)不能修改被装饰函数的调用方式;

########## 基本装饰器 ##########
def orter(func):    #定义装饰器
    def inner():
        print("This is inner before.")
        s = func()    #调用原传入参数函数执行
        print("This is inner after.")
        return s        #return原函数返回值
    return inner      #将inner函数return给name函数

@orter    #调用装饰器(将函数name当参数传入orter装饰器)
def name():
    print("This is name.")
    return True        #name原函数return True 

ret = name()
print(ret)

输出结果:
This is inner before.
This is name.
This is inner after.
True
View Code
############ 装饰器传参数 ###########
def orter(func):
    def inner(a,b):      #接收传入的2个参数
        print("This is inner before.")
        s = func(a,b)    #接收传入的原函数2个参数
        print("This is inner after.")
        return s
    return inner

@orter
def name(a,b):    #接收传入的2个参数,并name整体函数当参数传入orter装饰器
    print("This is name.%s,%s"%(a,b))
    return True

ret = name('nick','jenny')    #传入2个参数
print(ret)

输出结果:
This is inner before.
This is name.nick,jenny
This is inner after.
True
View Code
########## 万能参数装饰器 ##########
def orter(func):
    def inner(*args,**kwargs):        #万能参数接收多个参数
        print("This is inner before.")
        s = func(*args,**kwargs)       #万能参数接收多个参数
        print("This is inner after.")
        return s
    return inner

@orter
def name(a,b,c,k1='nick'):        #接受传入的多个参数
    print("This is name.%s,%s"%(a,b))
    return True

ret = name('nick','jenny','car')
print(ret)

输出结果:
This is inner before.
This is name.nick,jenny
This is inner after.
True
View Code
########### 一个函数应用多个装饰器 #########

def orter(func):
    def inner(*args,**kwargs):
        print("This is inner one before.")
        print("This is inner one before angin.")
        s = func(*args,**kwargs)
        print("This is inner one after.")
        print("This is inner one after angin.")
        return s
    return inner

def orter_2(func):
    def inner(*args,**kwargs):
        print("This is inner two before.")
        print("This is inner two before angin.")
        s = func(*args,**kwargs)
        print("This is inner two after.")
        print("This is inner two after angin.")
        return s
    return inner

@orter            #将以下函数整体当参数传入orter装饰器  
@orter_2          #将以下函数当参数传入orter_2装饰器  
def name(a,b,c,k1='nick'):
    print("This is name.%s and %s."%(a,b))
    return True

ret = name('nick','jenny','car')
print(ret)

输出结果:
This is inner one before.
This is inner one before angin.
This is inner two before.
This is inner two before angin.
This is name.nick and jenny.
This is inner two after.
This is inner two after angin.
This is inner one after.
This is inner one after angin.
True
View Code

实现装饰器知识储备:

函数即“变量”

定义一个函数相当于把函数体赋值给了函数名

1.函数调用顺序

与其他高级语言类似,python不允许在函数未声明之前,对其进行引用或者调用

错误示范:

def foo():
    print('in the foo')
    bar()
foo()

"""
NameError: name 'bar' is not defined
"""
View Code

正确示范:(注意,python为解释执行,函数foo在调用前已经声明了bar和foo,所以bar和foo无顺序之分)

def foo():
    print('in the foo')
    bar()
def bar():
    print('in the bar')
foo()

def bar():
    print('in the bar')
def foo():
    print('in the foo')
    bar()
foo()

"""
in the foo
in the bar
in the foo
in the bar
"""
View Code

2.高阶函数

满足下列条件之一就可称函数为高阶函数

  1.某一函数当做参数传入另一个函数中

  2.函数的返回值包含n个函数,n>0

高阶函数示范:

def bar():
    print('in the bar')
def foo(func):
    res=func()
    return res
foo(bar)
"""
in the bar
"""
View Code

3.内嵌函数和变量作用域

  在一个函数体内创建另一个函数,这种函数就叫内嵌函数。

嵌套函数:

def foo():
    def bar():
        print('in the bar')
    bar()
foo()
"""
in the bar
"""
View Code

局部作用域和全局做用域的访问顺序

x = 0
def grandpa():
    def dad():
        x = 2
        def son():
            x=3
            print(x)
        son()
    dad()
grandpa()
"""
3
"""
View Code

4.高阶函数+内嵌函数  ==》 装饰器

函数参数固定

def decorartor(func):
    def wrapper(n):
        print('starting')
        func(n)
        print('stopping')
    return wrapper

def test(n):
    print('in the test arg is %s' %n)
decorartor(test)('alex')
View Code

函数参数不固定

def decorartor(func):
    def wrapper(*args,**kwargs):
        print('starting')
        func(*args,**kwargs)
        print('stopping')
    return wrapper

def test(n,x=1):
    print('in the test arg is %s' %n)
decorartor(test)('alex',x=2222)
View Code

1.无参装饰器

import time
def decorator(func):
    def wrapper(*args,**kwargs):
        start_time=time.time()
        func(*args,**kwargs)
        stop_time=time.time()
        print("%s" %(stop_time-start_time))
    return wrapper

@decorator
def test(list_test):
    for i in list_test:
        time.sleep(0.1)
        print('-'*20,i)

#decorator(test)(range(10))
test(range(10))
View Code

2.有参装饰器

import time
def timer(timeout=0):
    def decorator(func):
        def wrapper(*args,**kwargs):
            start=time.time()
            func(*args,**kwargs)
            stop=time.time()
            print('run time is %s ' %(stop-start))
            print(timeout)
        return wrapper
    return decorator
@timer(2)
def test(list_test):
    for i in list_test:
        time.sleep(0.1)
        print ('-'*20,i)
  
#timer(timeout=10)(test)(range(10))
test(range(10))
View Code

3.终极装饰器

user,passwd = 'alex','abc123'
def auth(auth_type):
    print('auth func:',auth_type)
    def outer_wrapper(func):
        def wrapper(*args,**kwargs):
            print("wrapper func args:",*args,**kwargs)
            if auth_type=="local":
                username = input("Username:").strip()
                password = input("Password:").strip()
                if user == username and passwd == password:
                    res = func(*args,**kwargs)
                    print("---after authentication")
                    return res
                else:
                    exit("\033[31;1mInvalid username or password\033[0m")
            elif auth_type == "ldap":
                print('搞毛线ldap,不会......')

        return wrapper
    return outer_wrapper

def index():
    print("welcome to index page")
@auth(auth_type="local")    #home = wrapper()
def home():
    print("welcome to home page")
    return "from home"
@auth(auth_type="ldap")
def bbs():
    print("welcome to bbs page")

index()
print(home())#wrapper()
bbs()
View Code

http://www.cnblogs.com/suoning/p/5499812.html

原文地址:https://www.cnblogs.com/ttrrpp/p/6791476.html