python的map,reduce和zip

本文详细介绍了Python中的三个高阶函数:map、reduce和zip。包括它们的官方定义、应用场景及示例代码,并展示了如何利用这些函数进行矩阵转置等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


map函数的官方解释:
map ( functioniterable... )
Return an iterator that applies   function   to every item of   iterable , yielding the results. If additional   iterable   arguments are passed,   function   must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted. For cases where the function inputs are already arranged into argument tuples, see   itertools.starmap()
      以上是官方给出的解释,将函数function应用到iterable的每一个元素,返回一个iterator。
如果再输入一个iterable2的话,同样将函数应用到iterable2的每一个元素,但是函数function必须要能接受对应的参数。如果是多重迭代(比如:二维数组)map会在最短的那个iterable迭代完结束。
这是一个iterable的例子:
这是两个iterable的例子:
注: 其中函数function必须能够接受后面两个数组的参数。
reduce函数的官方解释:
python3之后,reduce函数被移到了functools里。如果直接使用会出现下图错误。

必须导进functools包,如下:
注: python2依然可以直接使用reduce函数 
functools. reduce ( functioniterable [initializer ] )
Apply   function   of two arguments cumulatively to the items of   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) . The left argument,   x , is the accumulated value and the right argument,   y , is the update value from the   sequence . If the optional   initializer   is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty. If   initializer   is not given and   sequence   contains only one item, the first item is returned.
Roughly equivalent to:
def reduce (function, iterable, initializer = None ):
     it = iter (iterable)
     if initializer is None :
          value = next (it)
     else :
          value = initializer
     for element in it:
          value = function(value, element)
     return value
看完这段reduce函数的定义就会非常明白了,看看怎么用:
当有initializer参数时:
为什么要先说map和reduce呐?因为map-reduce的名气可是非常的大,起源于谷歌的几篇论文,主要应用在并行计算中,他们对应的中文名叫Map(映射)和reduce(规约)。我当年玩Hadoop时候,在其框架下编程,可没少被他俩折磨。想再了解他们的爱恨情仇的可以看一下他们的故事。
zip的官方定义:
zip ( *iterables )
Make an iterator that aggregates elements from each of the iterables.
Returns an iterator of tuples, where the   i -th tuple contains the   i -th element from each of the argument sequences or iterables. The iterator stops when the shortest input iterable is exhausted. With a single iterable argument, it returns an iterator of 1-tuples. With no arguments, it returns an empty iterator. Equivalent to:
def zip ( * iterables):
# zip('ABCD', 'xy') --> Ax By
     sentinel = object ()
     iterators = [ iter (it) for it in iterables]
     while iterators:
          result = []
          for it in iterators:
               elem = next (it, sentinel)
               if elem is sentinel:
                    return
               result . append(elem)
     yield tuple (result)
The left-to-right evaluation order of the iterables is guaranteed. This makes possible an idiom for clustering a data series into n-length groups using   zip(*[iter(s)]*n) . This repeats the   same   iterator   n   times so that each output tuple has the result of   n   calls to the iterator. This has the effect of dividing the input into n-length chunks.
zip()   should only be used with unequal length inputs when you don’t care about trailing, unmatched values from the longer iterables. If those values are important, use   itertools.zip_longest()   instead.
zip()   in conjunction with the   *   operator can be used to unzip a list:


>>>
>>> x = [ 1 , 2 , 3 ]
>>> y = [ 4 , 5 , 6 ]
>>> zipped = zip ( x , y )
>>> list ( zipped ) [(1, 4), (2, 5), (3, 6)]
>>> x2 , y2 = zip ( * zip ( x , y ))
>>> x == list ( x2 ) and y == list ( y2 )
True


zip的解释就不累述了,理解这个函数的方法和reduce类似。我们看一个小例子。
通过使用map和zip结合,可以完成矩阵的转置,当然使用numpy也可以完成,不过这种写代码的方法也是很常用的。










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值