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


相关推荐

  • USB协议简介「建议收藏」

    USB协议简介「建议收藏」1.USB协议简介https://blog.csdn.net/songze_lee/article/details/776580942017年08月28日23:20:16songze_lee阅读数:22978版权声明:本文为博主原创文章,未经博主允许不得转载。https://blog.csdn.net/songze_lee/article/details/77658094…

    2022年6月25日
    28
  • Java封装详解,很简单

    Java封装详解,很简单大家好,今天来给大家分享一下Java封装面向对象有三大特征:1.封装2.继承3.多态我们来讲Java封装,它也是Java面向对象的三大特征之一封装,大白话的解释就是,把一个东西,装到箱子了,只留小小的口,用于外界访问画一个图就是该露的露,该藏的藏我们在程序设计的过程中要追求“高内聚,低耦合”。高内聚就是类的内部数据操作细节自己来完成,不允许外部干涉,低耦合:就是,仅暴露少量的方法给外部使用封装(数据的隐藏)通常,应禁止直接访问一个对象中数据的实际表示,而是应该通过操作接口来访问

    2022年7月7日
    29
  • vue中清除浏览器缓存得方法

    vue中清除浏览器缓存得方法1.在HTTP协议中,只有后端返回expires或Cache-Control:max-age=XXX,前端才缓存。但在浏览器中,默认会对htmlcssjs等静态文件、以及重定向进行缓存,如果在HEAD头中指定:<HEAD> <METAHTTP-EQUIV=”Pragma”CONTENT=”no-cache”> <METAHTTP-EQUIV=”Cache-Control”CONTENT=”no-cache”> <METAHTTP-EQUIV

    2022年7月18日
    12
  • python + selenium 刷B站播放量

    python + selenium 刷B站播放量

    2021年11月10日
    156
  • 微生物组-宏基因组分析第9期(报名直播课免费参加线下2020.10本年最后一期)

    微生物组-宏基因组分析第9期(报名直播课免费参加线下2020.10本年最后一期)福利公告:为了响应学员的学习需求,经过易生信培训团队的讨论筹备,现决定安排扩增子16S分析、宏基因组、Python课程和转录组的线上直播课。报名参加线上直播课的老师可在1年内选择参加同课…

    2022年5月29日
    34
  • HTML5期末大作业:旅游网页设计——西安旅游9页(代码质量好) 学生DW网页设计作业源码 web课程设计网页规划与设计

    HTML5期末大作业:旅游网页设计——西安旅游9页(代码质量好) 学生DW网页设计作业源码 web课程设计网页规划与设计HTML5期末大作业:网站——西安旅游9页(代码质量好)学生DW网页设计作业源码web课程设计网页规划与设计临近期末,你还在为HTML网页设计结课作业,老师的作业要求感到头大?HTML网页作业无从下手?网页要求的总数量太多?没有合适的模板?等等一系列问题。你想要解决的问题,在这篇博文中基本都能满足你的需求~原始HTML+CSS+JS页面设计,web大学生网页设计作业源码,这是一个不错的网页制作,画面精明,非常适合初学者学习使用。作品介绍1.网页作品简介方面:HTML期末大学生网页设计作业

    2022年4月30日
    56

发表回复

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

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