在3.3里,如果直接使用map(), filter(), reduce(), 会出现

>>> def f(x): return x % 2 != 0 and x % 3 != 0 
>>> filter(f, range(2, 25)) <</span>filter object at 0x0000000002C14908> 
>>> def cube(x): return x*x*x 
>>> map(cube, range(1, 11)) <</span>map object at 0x0000000002C82B70> >>> defadd(x,y): return x+y 
>>> reduce(add, range(1, 11)) Traceback (most recent call last): File "", line 1, in<</span>module> reduce(add, range(1, 11)) NameError: name 'reduce' is not defined
这种情况是因为在3.3里面,map(),filter()这些的返回值已经不再是list,而是iterators, 所以想要使用,只用将iterator 转换成list 即可, 比如  list(map()) 
而reduce已经取消了,如想使用,可以用fuctools.reduce来调用。但是要先导入fuctools, 即输入:import fuctools 
e.g1
>>> sentence = 'It is raining cats and dogs'

>>> words = sentence.split()
>>> print words

['It', 'is', 'raining', 'cats', 'and', 'dogs']

>>> 
>>> lengths = map(lambda word: len(word), words)
>>> print (list(lengths))

[2, 2, 7, 4, 3, 4]

e.g2
>>> import functools 
>>> def add(x,y): return x+y ... 
>>> functools.reduce(add, range(1, 11)) 55

如何在python3.3用 map filter reduce的更多相关文章

  1. python 内置函数 map filter reduce lambda

    map(函数名,可遍历迭代的对象) # 列组元素全加 10 # map(需要做什么的函数,遍历迭代对象)函数 map()遍历序列得到一个列表,列表的序号和个数和原来一样 l = [2,3,4,5,6, ...

  2. Swift map filter reduce 使用指南

    转载:https://useyourloaf.com/blog/swift-guide-to-map-filter-reduce/ Using map, filter or reduce to ope ...

  3. python常用函数进阶(2)之map,filter,reduce,zip

    Basic Python : Map, Filter, Reduce, Zip 1-Map() 1.1 Syntax # fun : a function applying to the iterab ...

  4. 数组的高阶方法map filter reduce的使用

    数组中常用的高阶方法: foreach    map    filter    reduce    some    every 在这些方法中都是对数组中每一个元素进行遍历操作,只有foreach是没有 ...

  5. 高阶函数map(),filter(),reduce()

    接受函数作为参数,或者把函数作为结果返回的函数是高阶函数,官方叫做 Higher-order functions. map()和filter()是内置函数.在python3中,reduce()已不再是 ...

  6. Python面试题之Python中的lambda map filter reduce zip

    当年龟叔想把上面列出来的这些都干掉.在 “All Things Pythonic: The fate of reduce() in Python 3000”这篇文章中,他给出了自己要移除lambda. ...

  7. lambda,map,filter,reduce

    lambda 编程中提到的 lambda 表达式,通常是在需要一个函数,但是又不想费神去命名一个函数的场合下使用,也就是指匿名函数.返回一个函数对象. func = lambda x,y:x+y fu ...

  8. Python map filter reduce enumerate zip 的用法

    map map(func, list) 把list中的数字,一个一个运用到func中,常和lambda一起用. nums = [1, 2, 3, 4, 5] [*map(lambda x: x**2, ...

  9. python map filter reduce的优化使用

    这篇讲下python中map.filter.reduce三个内置函数的使用方式,以及优化方法. map()函数 map()函数会根据提供的函数对指定序列做映射. 语法: map(function,it ...

随机推荐

  1. hashmap和hashtable,arraylist和vector的区别

    hashmap线程不安全,hashtable线程安全 hashmap允许使用 null 值和 null 键.(除了非同步和允许使用 null 之外,HashMap 类与 Hashtable 大致相同. ...

  2. hadoop shell 操作命令

    shell操作命令: hdfs的路径,在core-site.xml中定义 <property> <name>fs.default.name</name> <v ...

  3. pager分页框架体会

    <pg:pager> 元素的属性中: maxPageItems说的是每页偏移量是多少,这个并不是说每一页显示多少,而是第二页比第一页来说,在第一页的尾部增加多少,第一页又被覆盖多少,是决定 ...

  4. Mahout0.9的安装与测试

    最近想实协同过滤的MR算法,但是网上查了一下,发现hadoop的生态系统中的Mahout的项目已经实现了相应的算法,因此想先尝试着实时这个mahout的使用及效果.要想用mahout必须要部署到had ...

  5. php大力力 [031节] php设计系统后台菜单和样式设计

    php大力力 [031节] php设计系统后台菜单和样式设计 耗掉我一整夜的时间,把后台html设计了一个,对于我这样的html白痴,实属不容易啊. 留下一点点网上查找的网页知识: 索马里论坛群发简介 ...

  6. python3爬虫初探(五)之从爬取到保存

    想一想,还是写个完整的代码,总结一下前面学的吧. import requests import re # 获取网页源码 url = 'http://www.ivsky.com/tupian/xiaoh ...

  7. Error Handling and Exception

    The default error handling in PHP is very simple.An error message with filename, line number and a m ...

  8. Android布局居中的几种做法

    Android的布局文件中,如果想让一个组件(布局或View)居中显示在另一个布局(组件)中,可以由这么几种做法: android:layout_gravity android:gravity and ...

  9. 301、404、200、304、500等HTTP状态,代表什么意思?

    一些常见的状态码为: 200 - 服务器成功返回网页 404 - 请求的网页不存在 503 - 服务器超时 下面提供 HTTP 状态码的完整列表.点击链接可了解详情.您也可以访问 HTTP 状态码上的 ...

  10. Linux关机命令

    1.halt  :关机 init 0 : 关机 shutdown  -h  now (立刻关机)  -h 指的是  halt 2.reboot  重启 init 0 重启 shutdown -r no ...