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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 浙江更新了小学3年级到9年级信息技术课,小学开始学编程

    浙江更新了小学3年级到9年级信息技术课,小学开始学编程据浙江最新消息,今年9月份开始的新学期,三到九年级信息技术课将同步替换新器材。其中,八年级将新增Python课程内容。新高一信息技术编程语言由VB替换为Python,大数据、人工智能、程序设计与算法按照教材规划五六年级开始接触。在最新的教材目录上看到,从小学三年级一直到九年级,内容都有不同程度的调整。三年级新增了“信息社会”和“网络生活”,四上新增了“走进多媒体”、“制作演示文稿”和数字名片(H5),五下加入了“算法和程序设计”,六年级更是出现了“大数据”、“初始人工智能”、“万物互联”。初中阶段新

    2022年5月17日
    59
  • 最小二乘法进行线性回归_最小二乘法简单例题

    最小二乘法进行线性回归_最小二乘法简单例题最小二乘法概述对于一元线性回归模型,假设从总体中获取了n组观察值(x1,y1)(x1,y1)(x_1,y_1),(x2,y2)(x2,y2)(x_2,y_2),…,(xn,yn)(xn,yn)(x_n,y_n)。对于平面中的这n个点,可以使用无数条曲线来拟合。要求样本回归函数尽可能好地拟合这组值。综合起来看,这条直线处于样本数据的中心位置最合理。选择最佳拟合曲线的标准可以确定为:使总的拟…

    2022年9月25日
    5
  • 三菱modbus通讯实例 PLC如何设置_三菱plc网络通讯指令范例

    三菱modbus通讯实例 PLC如何设置_三菱plc网络通讯指令范例点击箭头处“工业之家”,选择“关注公众号”!三菱PLC控制机械手实例气动机械手动作示意图,其功能是将工件从A处移送到B处。气动机械手的升降和左右移行分别使用了双线圈的电磁阀,在某方向的驱动线圈失电时能保持在原位,必须驱动反方向的线圈才能反向运动。上升、下降对应的电磁阀线圈分别是YV2、YV1,右行、左行对应的电磁阀线圈分别是YV3、YV4。机械手的夹钳使用单线圈电磁阀YV5,线圈通电时夹…

    2025年10月20日
    6
  • CMD命令实现数字雨

    CMD命令实现数字雨使用cmd命令可以实现类似黑客帝国中的数字雨,这里给出脚本和演示效果:digitalrain.bat@echoofftitledigitalraincolor0bsetlocalENABLEDELAYEDEXPANSIONfor/l%%iin(0)do(set”line=”for/l%%jin(1,1,80)do(set/aDown%%…

    2022年6月9日
    49
  • Android Studio的Logcat/Run/Terminal/Build等窗口没有了怎么调出

    Android Studio的Logcat/Run/Terminal/Build等窗口没有了怎么调出

    2021年10月2日
    75
  • java的基础代码_java编程入门基础教程

    java的基础代码_java编程入门基础教程1.编写java源文件,认识java基本程序结构。创建一个文本文件,并重命名为”HelloWorld.java”用记事本打开,编写一段Java代码如下面所示例子所示。ClassHelloWorld.java{//main是程序的入口,所有程序都是从此处开始运行Publicstaticvoidmain(String[]arge){//在屏幕中打印输出“HelloWorld!”语句System.out.println(“HelloWorld”);}}2.下面对每条语句

    2022年10月17日
    4

发表回复

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

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