# -*- 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的更多相关文章

  1. Python内建函数reduce()用法

    reduce把一个函数作用在一个序列[x1, x2, x3...]上,这个函数必须接收两个参数,reduce把结果继续和序列的下一个元素做累积计算,下面讲述Python内建函数reduce()用法. ...

  2. python用reduce和map把字符串转为数字的方法

    python用reduce和map把字符串转为数字的方法 最近在复习高阶函数的时候,有一道题想了半天解不出来.于是上午搜索资料,看了下别人的解法,发现学习编程,思维真的很重要.下面这篇文章就来给大家介 ...

  3. python map, reduce,filter 使用

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

  4. python的reduce()函数

    reduce()函数也是Python内置的一个高阶函数. reduce()函数接收的参数和 map()类似,一个函数 f,一个list,但行为和 map()不同,reduce()传入的函数 f 必须接 ...

  5. python 中 reduce 函数的使用

    reduce()函数也是Python内置的一个高阶函数. reduce()函数接收的参数和 map()类似,一个函数 f,一个list,但行为和 map()不同,reduce()传入的函数 f 必须接 ...

  6. python的reduce()函数(转)

    reduce()函数也是Python内置的一个高阶函数. reduce()函数接收的参数和 map()类似,一个函数 f,一个list,但行为和 map()不同,reduce()传入的函数 f 必须接 ...

  7. 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, ...

  8. Python map,reduce,filter,apply

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

  9. python中reduce()函数

    reduce()函数也是Python内置的一个高阶函数.reduce()函数接收的参数和 map()类似,一个函数 f,一个list,但行为和 map()不同,reduce()传入的函数 f 必须接收 ...

随机推荐

  1. FT项目开发技术点(二)

    1.mybatis二级缓存,指的的是将数据缓存,而非对象,而非获得的list.缓存将数据库中的数据,是数据,缓存到内存中.之后将数据每次重新加载到list中,所以每次生成的list对象都是不同的,li ...

  2. 【docker】关于docker中挂载的解释

    现在有这么一个命令: docker run -p 33061:3306 --name mysql --restart=always -e MYSQL_ROOT_PASSWORD=pisen -v /e ...

  3. MSI failed, 不能卸载VMware

    解决方法; http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&ext ...

  4. [Android Pro] activity-alias的使用

    activity-alias是android里为了重复使用Activity而设计的. 当在Activity的onCreate()方法里,执行getIntent().getComponent().get ...

  5. 深度学习阅读列表 Deep Learning Reading List

    Reading List List of reading lists and survey papers: Books Deep Learning, Yoshua Bengio, Ian Goodfe ...

  6. Objective-C:NSValue类的常见用法

    特殊类型的包装类:数组.结构体(OC内部的.自定义的).指针 // // main.m // 05-NSValue // // Created by ma c on 15/8/17. // Copyr ...

  7. [leetcode]Single Number II @ Python

    原题地址:http://oj.leetcode.com/problems/single-number-ii/ 题意:Given an array of integers, every element ...

  8. Decorator Wrapper 装饰模式 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  9. EasyUI-Tooltip(提示框)学习

    引子: if($("#BLUETOOTH_a")){ $("#BLUETOOTH_a").tooltip({ position: 'right', conten ...

  10. Android -- 跳转应用市场评分

    Code Uri uri = Uri.parse("market://details?id="+getPackageName()); Intent intent = new Int ...