在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. SharePoint 2013 开发——发布SharePoint应用程序

    博客地址:http://blog.csdn.net/FoxDave 前几篇我们介绍了开发.部署和调试SharePoint应用程序的基础,本篇介绍更实用的操作,当我们开发一个SharePoint应用 ...

  2. 如何使用 PagedList.Mvc 分页

    刚开始找PagedList分页不是例子太复杂,就是写的过于简略,由于对于MVC的分页不太了解,之前使用的都是Asp.Net 第三方控件 + 数据库存储过程分页.还是老外写的例子简捷,https://g ...

  3. ajax注释

    //xmlHttpRequest,但是这个对象只是在火狐,google...//在中国用的最广泛的IE浏览器里面是没有这个对象的//在IE里面是用的一个控件来解决这个问题,ActiveXObject/ ...

  4. 文件操作II

    <html> <head> <meta charset="utf-8"> </head> <body> <?php ...

  5. vim的Tab设置为4个空格

    vim /etc/vimrc 1    set ts=42    set expandtab3    set autoindent 按tab键时产生的是4个空格,这种方式具有最好的兼容性.

  6. Ubuntu虚机中SVN连接出错,虚机本机可正常CO,CIN,解决方法

    Ubuntu虚机中SVN连接出错,虚机本机可正常CO,CIN,外面机器无法正常连接. 解决: 虚机换个IP即可正常连接,原因不明,有可能为公司网管对该IP做了某些限制. PS:VMware中只需将网络 ...

  7. 如何解决火狐FF里Input标签刷新页面后 仍然保存之前输入的内容的方法。

    直接在input 标签里 增加 autocomplete="off".火狐默认为 on.

  8. 【LeetCode OJ】Best Time to Buy and Sell Stock

    Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock/ We solve this problem ...

  9. Euro Efficiency_完全背包

    Description On January 1st 2002, The Netherlands, and several other European countries abandoned the ...

  10. 3、SQL基础整理(分组)

    分组 group by select class from xuesheng group by class select class,AVG(chinese)from xuesheng group b ...