如何在python3.3用 map filter reduce
在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的更多相关文章
- python 内置函数 map filter reduce lambda
map(函数名,可遍历迭代的对象) # 列组元素全加 10 # map(需要做什么的函数,遍历迭代对象)函数 map()遍历序列得到一个列表,列表的序号和个数和原来一样 l = [2,3,4,5,6, ...
- Swift map filter reduce 使用指南
转载:https://useyourloaf.com/blog/swift-guide-to-map-filter-reduce/ Using map, filter or reduce to ope ...
- python常用函数进阶(2)之map,filter,reduce,zip
Basic Python : Map, Filter, Reduce, Zip 1-Map() 1.1 Syntax # fun : a function applying to the iterab ...
- 数组的高阶方法map filter reduce的使用
数组中常用的高阶方法: foreach map filter reduce some every 在这些方法中都是对数组中每一个元素进行遍历操作,只有foreach是没有 ...
- 高阶函数map(),filter(),reduce()
接受函数作为参数,或者把函数作为结果返回的函数是高阶函数,官方叫做 Higher-order functions. map()和filter()是内置函数.在python3中,reduce()已不再是 ...
- Python面试题之Python中的lambda map filter reduce zip
当年龟叔想把上面列出来的这些都干掉.在 “All Things Pythonic: The fate of reduce() in Python 3000”这篇文章中,他给出了自己要移除lambda. ...
- lambda,map,filter,reduce
lambda 编程中提到的 lambda 表达式,通常是在需要一个函数,但是又不想费神去命名一个函数的场合下使用,也就是指匿名函数.返回一个函数对象. func = lambda x,y:x+y fu ...
- Python map filter reduce enumerate zip 的用法
map map(func, list) 把list中的数字,一个一个运用到func中,常和lambda一起用. nums = [1, 2, 3, 4, 5] [*map(lambda x: x**2, ...
- python map filter reduce的优化使用
这篇讲下python中map.filter.reduce三个内置函数的使用方式,以及优化方法. map()函数 map()函数会根据提供的函数对指定序列做映射. 语法: map(function,it ...
随机推荐
- jQuery 操作CSS
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 《JAVA学习笔记(1---13-4)》
[1]问题: 1.什么叫做面向过程? 2.什么叫做面向对象? 解答: 1: 所谓的面向过程就是我们是一个执行者,我们要开发一个项目,这个项目要求要实现很多功能,作为执行者的我们就需要 去一个一个的找这 ...
- Cookie 与Session 的区别(转载)
原地址: http://www.cnblogs.com/shiyangxt/archive/2008/10/07/1305506.html 两个都可以用来存私密的东西,同样也都有有效期的说法. 区别在 ...
- windows防火墙添加规则
#include <windows.h> #include <crtdbg.h> #include <netfw.h> #include <objbase.h ...
- 华为V-ISA信誉安全体系:对付新型DDoS攻击的利器
华为Anti-DDoS解决方案基于华为颇具传统优势的专业软硬件平台开发,在防护机制中,引入先进的检测机制,提供了业内首创的“V-ISA”信誉安全体系,是业界唯一单机可提供超百G DDoS防御能 ...
- 深入学习:Windows下Git入门教程(上)
一,安装Git: 1.1Linux上安装命令: sudo apt-get install git 1.2在Windows上安装Git: 使用Windows版的msysgit,官方下载地址:http:/ ...
- JAVA的数组和输入
package com.java1995; import java.util.Scanner; public class Array { public static void main(String[ ...
- 在android的spinner中,实现取VALUE值和TEXT值。 ZT
在android的spinner中,实现取VALUE值和TEXT值. 为了实现在android的 spinner实现取VALUE值和TEXT值,我尝试过好些办法,在网上查的资料,都是说修改适配器, ...
- HDOJ-三部曲-多重背包-1014-Cash Machine
通过这道题我基本了解了利用二进制对多重背包问题进行优化的思想. Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submiss ...
- susy-Toolkit 之翻译
Toolkit工具包 The Susy 2.0 toolkit is built around our shorthand syntax. Use the shorthand to control e ...