• python map reduce apply


    吕sir安排事情做了,不过打算先用python做个原型系统

    说实话,我没认真用过一次python

    传说python的map,reduce这些都是c语言级的优化,而写循环就会慢很多

    所以能用map,reduce的时候就用吧,尽量避免写循环

    map(fun , list)

    将list的每个元素作用于一元函数fun,并且返回所有fun值的列表

    reduce(function, sequence[, initial]) 

    将sequence作用于2元函数function,function有两个参数,第一个为之前运算的值

    最后返回一个值

    For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
    ((((1+2)+3)+4)+5).

    apply

    apply(object[, args[, kwargs]]) -> value

    Call a callable object with positional arguments taken from the tuple args,
    and keyword arguments taken from the optional dictionary kwargs.
    Note that classes are callable, as are instances with a __call__() method.

    Deprecated since release 2.3. Instead, use the extended call syntax:
    function(*args, **keywords).

    有个函数需要把一个图片拆分为大小相同的很多的小图片

    按照以前写程序的思想就是写两重循环了,枚举开始左上角的点。

    当然不是不可以,但是我们这里的目的是不写循环,所以来用用map,reduce函数

     1 def split_all_image(im ,size_box):
     2     """                                                                                                                                                      
     3     split the image into many subimages                                                                                                                      
     4     the subimage's size (x,y) is size_box                                                                                                                    
     5     type size_box = tuple                                                                                                                                    
     6     ex: split_all_image(im , (16,16))                                                                                                                        
     7     """
     8     max_x , max_y = im.size
     9     step_x , step_y = size_box
    10     max_x -= step_x
    11     max_y -= step_y
    12     ys = xrange(0 , max_y  , step_y)
    13     tmp = [map(lambda y : (x,y) , [y for y in ys]) for x in xrange(0 , max_x , step_x)]
    14     xy = reduce(lambda x , y : x + y , tmp)
    15     blocks = map(lambda x : get_signle_block(im , (x[0],x[1],x[0]+step_x,x[1]+step_y)) , xy)
    16     return blocks
    by 1957
  • 相关阅读:
    分享:两个栈实现一个队列的功能
    分享:要关注技术,但也要关注所做的事情
    linux网络编程中阻塞和非阻塞socket的区别
    分享:C++参数传递方式
    linux非阻塞socket教程
    有用和有趣的产品秤砣
    查找 grep r TFramedTransport *
    分享:SecureCRT使用VIM语法高亮
    linux 非阻塞 socket Google 搜索
    linux c语言 select函数用法 ITeye技术网站
  • 原文地址:https://www.cnblogs.com/x1957/p/2601648.html
Copyright © 2020-2023  润新知