Python lambda和reduce函数

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.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116036.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java递归和迭代的区别

    java递归和迭代的区别出现栈的溢出.而迭代不会!  递归的基本概念:程序调用自身的编程技巧称为递归,是函数自己调用自己.一个函数在其定义中直接或间接调用自身的一种方法,它通常把一个大型的复杂的问题转化为一个与原问题相似的规模较小的问题来解决,可以极大的减少代码量.递归的能力在于用有限的语句来定义对象的无限集合.使用递归要注意的有两点:1)递归就是在过程或函数里面调用自身;2)在使用递归时,必须有一个明确的递归结束条件,称为递归出口. 递归分为两个阶段:1)递推:把复杂的问题的求解推到比原问

    2022年5月5日
    48
  • Linux设备树详解(一) 基础知识

    Linux设备树详解(一) 基础知识1.前言关于设备树,之前就经过详细的系统培训,但是本着会用就行的原则,对各个知识点都没有进行系统的总结。都是用到哪里学哪里,时间长了,基本也忘记了。所以对于后期知识各个知识点进行总结。2.为什么要引入DTS在传统Linux内核中,ARM架构的板极硬件细节过多地被硬编码在arch/arm/plat-xxx和arch/arm/mach-xxx,比如板上的platform设备、resource…

    2022年6月16日
    27
  • 充分条件和必要条件的口诀_充分必要条件的例子100个

    充分条件和必要条件的口诀_充分必要条件的例子100个充分条件:如果条件A是结论B的充分条件:A与其他条件是并连关系,即A、C、D….中任意一个存在都可以使得B成立(就像是个人英雄主义),如下图:<imgsrc="https://p

    2022年8月6日
    6
  • 数据库中的schema

    数据库中的schema数据库中的schema

    2022年4月25日
    48
  • 面向对象程序设计的基本概念_java面向对象程序设计

    面向对象程序设计的基本概念_java面向对象程序设计Java程序设计(面向对象)- 基本概念

    2022年4月22日
    54
  • 操作系统进程调度算法(c语言实现)

    操作系统进程调度算法(c语言实现)进程调度算法一、先来先服务(FCFS)基本思想:先到达的进程先进入就绪队列,先进行调度的原则。非抢占方式。二、短作业优先(SJF)基本思想:根据进程中的执行时间,选取执行时间最短的作业优先调度;可有抢占或非抢占方式。三、优先权高者优先(HPF)基本思想:系统根据作业的优先权进行作业调度,每次选取优先权高的作业优先调度。作业的优先权通常用一个整数表示,也叫做优先数。可有抢占或非抢占方式。…

    2025年8月25日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号