Python lambda和reduce函数

看到一篇博文写lambda和reduce函数,笔者小痒了一下,用Python实现一下:

#! /usr/bin/env python

# -*-coding:utf-8-*-

import time

import math

def test_reduce():

start_time = time.clock()

print reduce[A1] (lambdax,y:x*y[A2] ,range(1,long(input(‘plz
input a num(>0):‘)+1))
[A3] )

print ‘Time used:%s‘ %(time.clock()-start_time)

return;

def test_math():

start_time2 = time.clock()

print math.factorial[A4] (long(raw_input(‘plz
input a num(>0):‘)))

print ‘Time used:%s‘ %(time.clock()-start_time2)

if __name__ == ‘__main__‘:

print ‘~‘*34+‘Use reduce‘+‘~‘*34

test_reduce()

print ‘~‘*34+‘Use math‘+‘~‘*34

test_math()

Python is Python!


[A1]关于reduce函数的参数及解释:

reduce(function, iterable[, initializer])

Apply function of two argumentscumulatively to the items of iterable, from left to right, so as to reduce theiterable to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4,5]) calculates ((((1+2)+3)+4)+5). The left argument, x, is the accumulatedvalue
and the right argument, y, is the update value from the iterable. If theoptional initializer is present, it is placed before the items of the iterablein the calculation, and serves as a default when the iterable is empty. Ifinitializer is not given and iterable
contains only one item, the first item isreturned.

[A2]关于lambda函数的参数及解释:

An anonymous inline function consisting ofa single expressionwhich is evaluated when the function is called. The syntax to create a lambdafunction is lambda [arguments]: expression

Lambda expressions (sometimes called lambdaforms) have the same syntactic position as expressions. They are a shorthand tocreate anonymous functions; the expression lambda arguments: expression yieldsa function object. The unnamed object behaves like a function
object definedwith

def name(arguments):

return expression

[A3]关于range函数的参数及解释:

the built-in function range() returns a sequence of integers suitable to emulate theeffect of Pascal’s for i := a to b do; e.g., range(3) returns the list [0, 1,2].

[A4]关于factorial函数的参数及解释:

math.factorial(x)

Return x factorial. Raises ValueError if x is not integral or is negative.

Python lambda和reduce函数

时间: 2024-08-16 06:55:24

Python lambda和reduce函数的相关文章

python进阶一(函数式编程)【2-3 python中的reduce函数】

2-3 python中的reduce函数 python中reduce()函数 reduce()函数也是Python内置的一个高阶函数.reduce()函数接收的参数和 map()类似,一个函数 f,一个list,但行为和 map()不同,reduce()传入的函数 f 必须接收两个参数,reduce()对list的每个元素反复调用函数f,并返回最终结果值. 例如,编写一个f函数,接收x和y,返回x和y的和: 1 def f(x, y): 2 return x + y 调用 reduce(f, [

python中filter(),reduce()函数

filter()函数 是 Python 内置的另一个有用的高阶函数,filter()函数接收一个函数 和一个list,这个函数的作用是对每个元素进行判断,返回 True或 False,filter()根据判断结果自动过滤掉不符合条件的元素,返回由符合条件元素组成的新list. 例如:要从一个list [1,3,4,5,6,7,9,10]中删除偶数,保留奇数,编写一个判断奇数的函数: def is_odd(x): return x%2==1 a=list(filter(is_odd,[1,3,4,

python Lambda, filter, reduce and map

1. lambda The lambda operator or lambda function is a way to create small anonymous functions , i.e. functions without a name. 可以方便的创造一个函数.比如 def add(x,y): return x+y 用lambda 写就是 lambda x,y:x+y 非常简洁.这条语句会返回一个函数指针,你可以他赋值,或者配合map ,reduce 等操作. 比如说想把list

Python常用内置函数整理(lambda,reduce,zip,filter,map)

匿名函数lambda lambda argument1,argument2,...argumentN :expression using arguments 1.lambda是一个表达式,而不是一个语句. 因为这一点,lambda可以出现在python语法不允许def出现的地方---例如,在一个列表常量中或者函数调用的参数中. 2.lambda 的主体是一个单个的表达式,而不是一个代码块. lambda是一个为编写简单的函数设计的,而def用来处理更大的任务. 例如: lambda表示式写法:f

lambda&filter&map&reduce函数的基本使用

'''Created on 2019-03-14Author:BinzhouProject:python中lambda filter map reduce函数使用总结''' #lambda匿名函数,格式lambda x,y:x+y 表示一个函数的参数是x,y,函数实现功能是x+y #filter[function,sequence]用于过滤序列 返回迭代器对象(包含所有满足使function返回值为True的sequence值)#python2直接返回与sequence一致的数据类型(列表,元组

Python之reduce函数使用示例

#!/usr/bin/env python # -*- coding:utf8 -*- '''reduce:处理一个序列,然后把序列进行合并操作''' ###在python中没有reduce函数,所以需要导入它(去掉前面的注释符即可) #from functools import reduce def reduce_test(f,array,i = None): if i is None: tmp = array.pop(0) else: tmp = i for num in array: tm

Python3版本中的filter函数,map函数和reduce函数

一.filter函数: filter()为已知的序列的每个元素调用给定的布尔函数,调用中,返回值为非零的元素将被添加至一个列表中 1 def f1(x): 2 if x>20: 3 return True 4 else: 5 return False 6 7 l1 = [ 1, 2, 3, 42, 67, 16 ] 8 print(filter(f1, l1)) 9 #输出如下: 10 #<filter object at 0x000000000117B898> 11 l2 = filt

python之lambda,filter,map,reduce函数

g = lambda x:x+1 看一下执行的结果: g(1) >>>2 g(2) >>>3 当然,你也可以这样使用: lambda x:x+1(1) >>>2 可以这样认为,lambda作为一个表达式,定义了一个匿名函数,上例的代码x为入口参数,x+1为函数体,用函数来表示为: def g(x): return x+1 非常容易理解,在这里lambda简化了函数定义的书写形式.是代码更为简洁,但是使用函数的定义方式更为直观,易理解. Python中,

python几个特别函数map filter reduce lambda

lambda函数也叫匿名函数,即,函数没有具体的名称.先来看一个最简单例子: def f(x): return x**2 print f(4) Python中使用lambda的话,写成这样 g = lambda x : x**2 print g(4) lambda表达式在很多编程语言都有对应的实现.比如C#: var g = x => x**2 Console.WriteLine(g(4)) 那么,lambda表达式有什么用处呢?很多人提出了质疑,lambda和普通的函数相比,就是省去了函数名称