Python之reduce
# -*- coding: utf-8 -*-
#python 27
#xiaodeng
#Python之reduce
#http://python.jobbole.com/82597/ #1)reduce语法格式:
'''
reduce(...)
reduce(function, sequence[, initial]) -> value Apply a function of two arguments cumulatively to the items of a sequence,
from left to right, so as to reduce the sequence to a single value.
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5). If initial is present, it is placed before the items
of the sequence in the calculation, and serves as a default when the
sequence is empty.
'''
#function:函数名,函数自身能接收两个参数,决定了sequence中元素的作用方式,是乘法还是除法等等
#sequence:序列
#initial:累积初始值;如果给出initial, 则第一次传递initial和sequence的第一个元素给function. #2)reduce具体用法案例
n=4
print reduce(lambda x,y:x*y,range(1,n+1))# #等价于:
k=range(1,n+1)
def func(x,y):
return x*y print reduce(func,k)# #添加第三个参数
n=4
print reduce(lambda x,y:x*y,range(1,n+1),n)#
#4*1*2*3*4
#首先将第三个参数n和range(1,n+1)的第一个参数传递给func函数。
Python之reduce的更多相关文章
- Python内建函数reduce()用法
reduce把一个函数作用在一个序列[x1, x2, x3...]上,这个函数必须接收两个参数,reduce把结果继续和序列的下一个元素做累积计算,下面讲述Python内建函数reduce()用法. ...
- python用reduce和map把字符串转为数字的方法
python用reduce和map把字符串转为数字的方法 最近在复习高阶函数的时候,有一道题想了半天解不出来.于是上午搜索资料,看了下别人的解法,发现学习编程,思维真的很重要.下面这篇文章就来给大家介 ...
- python map, reduce,filter 使用
参考python built-on function: http://docs.python.org/2.7/library/functions.html?highlight=map%20reduce ...
- python的reduce()函数
reduce()函数也是Python内置的一个高阶函数. reduce()函数接收的参数和 map()类似,一个函数 f,一个list,但行为和 map()不同,reduce()传入的函数 f 必须接 ...
- python 中 reduce 函数的使用
reduce()函数也是Python内置的一个高阶函数. reduce()函数接收的参数和 map()类似,一个函数 f,一个list,但行为和 map()不同,reduce()传入的函数 f 必须接 ...
- python的reduce()函数(转)
reduce()函数也是Python内置的一个高阶函数. reduce()函数接收的参数和 map()类似,一个函数 f,一个list,但行为和 map()不同,reduce()传入的函数 f 必须接 ...
- 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, ...
- Python map,reduce,filter,apply
map(function, iterable, ...) map()函数接收两个参数,一个是函数,一个是可迭代的对象,map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回. 基本等 ...
- python中reduce()函数
reduce()函数也是Python内置的一个高阶函数.reduce()函数接收的参数和 map()类似,一个函数 f,一个list,但行为和 map()不同,reduce()传入的函数 f 必须接收 ...
随机推荐
- 页面中基于JSTL标签调用函数--之${fn:}内置函数
调用这样一个头文件<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions " ...
- synchronized与lock,哪个效率更高
Java在一开始就提供了synchronized关键字,用于多线程之间的同步.它使用简便,不会出现拿锁之后不归还的情况,可以避免一些编程错误. 而jdk5时提供的concurrent包里,有一个Loc ...
- 如何高效把一字节的位对换, bit0和bit7,bit1和bit6,以此类推.
#include<stdio.h> #include<stdlib.h> //异或法 unsigned char byteReverse(unsigned char val) ...
- SpringMVC处理方法的数据绑定
一.SpringMVC数据绑定流程 Spring MVC通过反射机制对目标处理方法的签名进行解析,将请求消息中的信息以一定的方式转换并绑定到处理方法的入参中.数据绑定的核心部件是DataBinder, ...
- parameter server学习
关于parameter server的学习: https://www.zybuluo.com/Dounm/note/517675 机器学习系统相比于其他系统而言,有一些自己的独特特点.例如: 迭代性: ...
- ubuntu 12.04 安装无线网卡驱动
安装ubuntu 12.04后,无线网卡不可用,采用以下方式解决: 1.在终端中运行如下命令,重新安装b43相关的全部驱动和firmware: sudo apt-get install bcmwl-k ...
- mahout源码分析之DistributedLanczosSolver(五)Job over
Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit. 1. Job 篇 接上篇,分析到EigenVerificationJob的run方法: public i ...
- SCSI contrller的几种类型的区别
在VMware vSphere Web Client中, 可以为虚拟机添加一个新的SCSI controller, 选项中包含如下的类型, 那么他们有什么区别呢? 如何选择呢? BusLogic ...
- AS 代码模板 文件模板 Templates MD
修改 File and Code Templates Settings –> Editor –>[File and Code Templates] 或者在右键new时选择子菜单[Edite ...
- Windows环境搭建Red5流媒体服务器
Windows环境搭建Red5流媒体服务器指南 测试环境:Windows 7 一. 下载安装程序 red5-server 下载地址 https://github.com/Red5/red5-ser ...