2017-07-31 18:20:59

一、map函数

map():会根据提供的函数对指定序列做映射。第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的iterator,即迭代器,使用list函数可以将之转成列表。

  • map(function, iterable, ...)

    • function -- 函数
    • iterable -- 一个或多个序列
def f(x):
return x ** 2 print(type(map(f,[1,2,3])))
print(list(map(f,[1,2,3]))) # <class 'map'>
# [1, 4, 9]

map()作为高阶函数,事实上它把运算规则抽象了,因此,我们不但可以计算简单的 f(x)=x2,还可以计算任意复杂的函数,比如,把这个list所有数字转为字符串等。

二、reduce函数

reduce():函数会对参数序列中元素进行累积。函数将一个数据集合(链表,元组等)中的所有数据进行下列操作,用传给reduce中的函数 function(有两个参数)先对集合中的第 1、2 个元素进行操作,得到的结果再与第三个数据用 function 函数运算,最后得到一个结果。

reduce把一个函数作用在一个序列[x1, x2, x3, ...]上,这个函数必须接收两个参数,reduce把结果继续和序列的下一个元素做累积计算,其效果就是:

reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)
  • reduce(function, iterable[, initializer]):

    • function -- 函数,有两个参数
    • iterable -- 可迭代对象
    • initializer -- 可选,初始参数
from functools import reduce

def add(x,y):
return x+y print(reduce(add,list(range(10)))) # 45

可以使用reduce来进行str转int

from functools import reduce

def str2int(s):
def fn(x, y):
return x * 10 + y
def char2num(s):
return {'': 0, '': 1, '': 2, '': 3, '': 4, '': 5, '': 6, '': 7, '': 8, '': 9}[s]
return reduce(fn, map(char2num, s))

Python map/reduce的更多相关文章

  1. python map, reduce,filter 使用

    参考python built-on function: http://docs.python.org/2.7/library/functions.html?highlight=map%20reduce ...

  2. Python map/reduce/filter/sorted函数以及匿名函数

    1. map() 函数的功能: map(f, [x1,x2,x3]) = [f(x1), f(x2), f(x3)] def f(x): return x*x a = map(f, [1, 2, 3, ...

  3. Python map,reduce,filter,apply

    map(function, iterable, ...) map()函数接收两个参数,一个是函数,一个是可迭代的对象,map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回. 基本等 ...

  4. Demo of Python &quot;Map Reduce Filter&quot;

    Here I share with you a demo for python map, reduce and filter functional programming thatowned by m ...

  5. python基础——map/reduce

    python基础——map/reduce Python内建了map()和reduce()函数. 如果你读过Google的那篇大名鼎鼎的论文“MapReduce: Simplified Data Pro ...

  6. map/reduce of python

    [map/reduce of python] 参考: http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac92 ...

  7. [python基础知识]python内置函数map/reduce/filter

    python内置函数map/reduce/filter 这三个函数用的顺手了,很cool. filter()函数:filter函数相当于过滤,调用一个bool_func(只返回bool类型数据的方法) ...

  8. 基于python的《Hadoop权威指南》一书中气象数据下载和map reduce化数据处理及其可视化

    文档内容: 1:下载<hadoop权威指南>中的气象数据 2:对下载的气象数据归档整理并读取数据 3:对气象数据进行map reduce进行处理 关键词:<Hadoop权威指南> ...

  9. Python map,filter,reduce函数

    # -*- coding:utf-8 -*- #定义一个自己的map函数list_list = [1,2,4,8,16] def my_map(func,iterable): my_list = [] ...

随机推荐

  1. bootstrap modal 垂直居中对齐

    bootstrap modal 垂直居中对齐   文章参考 http://www.bubuko.com/infodetail-666582.html http://v3.bootcss.com/Jav ...

  2. 原生http模块与使用express框架对比

    node的http创建服务与利用Express框架有何不同 原生http模块与使用express框架对比: const http = require("http"); let se ...

  3. Object-C-Foundation-set

    无序集合 哈希表 NSSet *colors=[NSSet setWithObjects:@@"yellow",@"red",@"blue" ...

  4. (五)使用GitHub的前期准备

    创建账户 创建账号成功后将得到一个个人的公开页面URL:https://github.com/xkfx. 设置头像 设置SSH Key SSH 为 Secure Shell 的缩写. from bai ...

  5. CSS style 属性

    CSS style 属性 定义和用法 必需的 type 属性规定样式表的 MIME 类型. type 属性指示 <style> 与 </style> 标签之间的内容. 值 &q ...

  6. Github使用教程(二)------ Github客户端使用方法

    在上一篇教程中,我们简单介绍了Github网站的各个部分,相信大家对Github网站也有了一个初步的了解(/(ㄒoㄒ)/~~可是还是不会用怎么办),不要着急,我们今天先讲解一下Github for w ...

  7. Python 自学基础(四)——time模块,random模块,sys模块,os模块,loggin模块,json模块,hashlib模块,configparser模块,pickle模块,正则

    时间模块 import time print(time.time()) # 当前时间戳 # time.sleep(1) # 时间延迟1秒 print(time.clock()) # CPU执行时间 p ...

  8. Cocos 开发笔记

    经发现: cocos creator 提供的hello world 模版中.只有HelloWorkd.js中 properties 属性 text的值不是'hello world!' Label 组件 ...

  9. [c/c++]指针(1)

    指针算是一种特殊的变量,它存储的是一个内存地址,你既可以通过它来给制定内存地址赋值 (可能会被操作系统收回或者与其它程序冲突),又可以提高运行速度 First Step: 声明一个指针变量 int* ...

  10. 乘积尾零|2018年蓝桥杯B组题解析第三题-fishers

    标题:乘积尾零 如下的10行数据,每行有10个整数,请你求出它们的乘积的末尾有多少个零? 5650 4542 3554 473 946 4114 3871 9073 90 4329 2758 7949 ...