• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > 跟老齐学Python之大话题小函数(2)

跟老齐学Python之大话题小函数(2)

作者:hebedich 字体:[增加 减小] 来源:互联网

hebedich 通过本文主要向大家介绍了跟老齐学python,跟老齐学python pdf,跟老齐学python下载,老齐 python,零基础学python 老齐等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

上一讲和本讲的标题是“大话题小函数”,所谓大话题,就是这些函数如果溯源,都会找到听起来更高大上的东西。这种思维方式绝对我坚定地继承了中华民族的优良传统的。自从天朝的臣民看到英国人开始踢足球,一直到现在所谓某国勃起了,都一直在试图论证足球起源于该朝的前前前朝的某国时代,并且还搬出了那时候的一个叫做高俅的球星来论证,当然了,勃起的某国是挡不住该国家队在世界杯征程上的阳痿,只能用高俅来意淫一番了。这种思维方式,我是坚定地继承,因为在我成长过程中,它一直被奉为优良传统。阿Q本来是姓赵的,和赵老爷是本家,比秀才要长三辈,虽然被赵老爷打了嘴。

废话少说,书接前文,已经研究了map,下面来看reduce。

忍不住还得来点废话。不知道看官是不是听说过MapReduc,如果没有,那么Hadoop呢?如果还没有,就google一下。下面是我从维基百科上抄下来的,共赏之。

MapReduce是Google提出的一个软件架构,用于大规模数据集(大于1TB)的并行运算。概念“Map(映射)”和“Reduce(化简)”,及他们的主要思想,都是从函数式编程语言借来的,还有从矢量编程语言借来的特性。
</div>

不用管是不是看懂,总之又可以用开头的思想意淫一下了,原来今天要鼓捣的这个reduce还跟大数据有关呀。不管怎么样,你有梦一般的感觉就行。

reduce

回到现实,清醒一下,继续敲代码:
>>> reduce(lambda x,y: x+y,[1,2,3,4,5])
15
</div>

 请看官仔细观察,是否能够看出是如何运算的呢?画一个图:

还记得map是怎么运算的吗?忘了?看代码:
>>> list1 = [1,2,3,4,5,6,7,8,9]
>>> list2 = [9,8,7,6,5,4,3,2,1]
>>> map(lambda x,y: x+y, list1,list2)
[10, 10, 10, 10, 10, 10, 10, 10, 10]
</div>

 看官对比一下,就知道两个的区别了。原来map是上下运算,reduce是横着逐个元素进行运算。

权威的解释来自官网:

reduce(function, iterable[, initializer])
 
Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable 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 iterable. If the optional initializer is present, it is placed before the items of the iterable in the calculation, and serves as a default when the iterable is empty. If initializer is not given and iterable contains only one item, the first item is returned. Roughly equivalent to:
</div>

   def reduce(function, iterable, initializer=None):
    it = iter(iterable)
    if initializer is None:
        try:
            initializer = next(it)
        except StopIteration:   
            raise TypeError('reduce() of empty sequence with no initial value')   
    accum_value = initializer                                                                  
    for x in it:
        accum_value = function(accum_value, x)   
    return accum_value
</div>

 如果用我们熟悉的for循环来做上面reduce的事情,可以这样来做:
>>> lst = range(1,6)
>>> lst
[1, 2, 3, 4, 5]
>>> r = 0
>>> for i in range(len(lst)):
...     r += lst[i]
...
>>> r
15
</div>

 for普世的,reduce是简洁的。

为了锻炼思维,看这么一个问题,有两个list,a = [3,9,8,5,2],b=[1,4,9,2,6],计算:a[0]b[0]+a1b1+...的结果。
>>> a
[3, 9, 8, 5, 2]
>>> b
[1, 4, 9, 2, 6]

>>> zip(a,b)        #复习一下zip,下面的方法中要用到
[(3, 1), (9, 4), (8, 9), (5, 2), (2, 6)]

>>> sum(x*y for x,y in zip(a,b))    #解析后直接求和
133

>>> new_list = [x*y for x,y in zip(a,b)]    #可以看做是上面方法的分布实施
>>> #这样解析也可以:new_tuple = (x*y for x,y in zip(a,b))
>>> new_list
[3, 36, 72, 10, 12]
>>> sum(new_list)     #或者:sum(new_tuple)
133

>>> reduce(lambda sum,(x,y): sum+x*y,zip(a,b),0)    #这个方法是在耍酷呢吗?
133

>>> from operator import add,mul            #耍酷的方法也不止一个
>>> reduce(add,map(mul,a,b))
133

>>> reduce(lambda x,y: x+y, map(lambda x,y: x*y, a,b))  #map,reduce,lambda都齐全了,更酷吗?
133
</div>

 filter

filter的中文含义是“过滤器”,在python中,它就是起到了过滤器的作用。首先看官方说明:

filter(function, iterable)

Construct a list from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. If iterable is a string or a tuple, the result also has that type; otherwise it is always a list. If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.

Note that filter(function, iterable) is equivalent to [item for item in iterable if function(item)] if function is not None and [item for item in iterable if item] if function is None.
</div>

这次真

您可能想查找下面的文章:

  • 跟老齐学Python之使用Python查询更新数据库
  • 跟老齐学Python之使用Python操作数据库(1)
  • 跟老齐学Python之编写类之二方法
  • 跟老齐学Python之编写类之一创建实例
  • 跟老齐学Python之关于类的初步认识
  • 跟老齐学Python之传说中的函数编写条规
  • 跟老齐学Python之总结参数的传递
  • 跟老齐学Python之变量和参数
  • 跟老齐学Python之重回函数
  • 跟老齐学Python之Python文档

相关文章

  • Flask的图形化管理界面搭建框架Flask-Admin的使用教程
  • python使用内存zipfile对象在内存中打包文件示例
  • pydev使用wxpython找不到路径的解决方法
  • Python中使用中文的方法
  • 讲解Python中运算符使用时的优先级
  • Python3.x中自定义比较函数
  • python实现的一个火车票转让信息采集器
  • Python实现的检测网站挂马程序
  • Python实现脚本锁功能(同时只能执行一个脚本)
  • python学习手册中的python多态示例代码

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • python使用Tkinter显示网络图片的方法
    • python查询mysql中文乱码问题
    • 在Debian下配置Python+Django+Nginx+uWSGI+MySQL的教程
    • python从入门到精通(DAY 2)
    • Python中return语句用法实例分析
    • 用Python抢过年的火车票附源码
    • 编写Python脚本抓取网络小说来制作自己的阅读器
    • Python实现telnet服务器的方法
    • Python天气预报采集器实现代码(网页爬虫)
    • 用smtplib和email封装python发送邮件模块类分享

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有