python 中的map , zip , reduce 都是神马玩意?

 
 

map  迭代
 
我们查看map函数的源码:
 
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.

 

先看一个例子:
>>> bb = map(
...     lambda a,b:(a,b) ,
...     [1,2,3,4],
...     "abc"
... )
>>>
>>> list(bb)
[(1, 'a'), (2, 'b'), (3, 'c')]

 

 
结论:
  1. map中第一个参数是一个函数
  2. map中第二个参数开始都是函数的参数,而且都是iterable的
  3. map会以最短数据源为截止点
 
 
 

 
zip ——— 聚合
 
Zip的定义:
 
class zip(object)
|  zip(iter1 [,iter2 [...]]) --> zip object
|  
|  Return a zip object whose .__next__() method returns a tuple where
|  the i-th element comes from the i-th iterable argument.  The .__next__()
|  method continues until the shortest iterable in the argument sequence
|  is exhausted and then it raises StopIteration.
 
平行从多个数据源接受数据,聚合成元组,知道最短数据源迭代结束,常用来构建字典
>>> result = zip(
...     [1,2,3,4],
...     "abc"
... )
>>> list(result)
[(1, 'a'), (2, 'b'), (3, 'c’)]
 
 
当然,你要可以用来构建字典
>>> result = zip(
...     [1,2,3,4],
...     "abc"
... )
>>> dict(result)
{1: 'a', 2: 'b', 3: 'c'}

 

 
 
Itertools.zip_longest   
如果要以最长的数据源为准,则可以使用 itertools.zip_longest
 
>>> from itertools import zip_longest
>>>
>>> result = zip_longest(
...     [1,2,3,4],
...     "abc"
... )
>>> list(result)
[(1, 'a'), (2, 'b'), (3, 'c'), (4, None)]
>>>

 

Zip 是可以对多个迭代器进行聚合的,看下面的例子,对3个可迭代对象进行zip操作
>>> result = zip(
...     [1,2,3,4],
...     [11,22,33],
...     "abc"
... )
>>>
>>> list(result)
[(1, 11, 'a'), (2, 22, 'b'), (3, 33, 'c')]
>>>

 

 
 
 

reduce  ——— 累积
迭代数据源,将结果带入下次的计算,这适合完成统计或过滤操作。
 
同样,先看定义:
Help on built-in function reduce in module _functools:
 
reduce(...)
    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.

 

解释:
  1. reduce   是存在于  functools模块中的,要使用它,需要先导入  functools模块
  2. reduce  有三个参数
                  function :   传入的参数函数(需要有2个形参)
                  sequence :    需要计算的队列,可以是(list , tuple , dict , set , string)
                  initiall  :         初始值
  3. 一次会传两个值进入到参数函数中去
  4. reduce  返回的是一个累计相加的结果,而非一个列表,所以它作用的是整个列表,而非列表中的元素
 
 
例子1:  不带初始值
 
>>> from functools import reduce
>>>
>>> reduce(
...     lambda x,y:x+y,
...     [1,2,3,4]
... )
10
>>>

 

例子2:  带初始值,既从什么数开始累加
>>> from functools import reduce
>>>
>>> reduce(
...     lambda x,y:x+y,
...     [1,2,3,4]
... )
10
>>>

 

      
说明:
     相加的过程是  
       100 + 1    ===>   101
       101 + 2    ===>   103
       103 + 3   ===>   106 
       103 + 4   ===>   110
 
 
当sequence  为  dict  时:
>>> from functools import reduce
>>>
>>> reduce(
...     lambda x,y:x+y,
...     [1,2,3,4]
... )
10
>>>

 

说明:
        只会对dict 中的key进行计算
 
当sequence 为  str  时:
 
>>> from functools import reduce
>>>
>>> reduce(
...     lambda x,y:x+y,
...     [1,2,3,4]
... )
10
>>>

 

 
 

zip与map的联系
    Zip 的功能完全是可以通过map 来实现的,看下面的例子
 
>>> list(
...     zip(
...             [1,2,3,4],
...             "abc"
...     )
... )
[(1, 'a'), (2, 'b'), (3, 'c')]
>>>
>>> list(
...     map(
...             lambda a,b:(a,b),  # 与 zip使用方式上的不同之处
...             [1,2,3,4],
...             "abc"
...     )
... )
[(1, 'a'), (2, 'b'), (3, 'c')]
>>>

 

 
 
 
 
 
 
posted @ 2019-07-08 14:28  大步向前blue  阅读(232)  评论(0编辑  收藏  举报