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)
上一篇 2022年1月26日 下午10:00
下一篇 2022年1月26日 下午11:00


相关推荐

  • 电脑蓝屏0x000000f4解决步骤_0x000000c4开机就蓝屏

    电脑蓝屏0x000000f4解决步骤_0x000000c4开机就蓝屏电脑蓝屏的问题是大家最常见到的电脑问题之一,大多时候蓝屏故障的出现都和软件有关,少数为硬件不兼容或者故障导致。这里粗略的分析下STOP:0x000000F4字段的问题,仅做参考!以下先来看看网友是怎么分析与解决问题的吧!蓝屏代码0x000000f4原因分析:知道了原因,那么就下手解决类似问题就轻松了。1.先从软处着手,如运行莫软件导致错误,建议重装该软件,一般均可解决。2.考虑是否有其它软件同时运行导致冲突的或者主机配置内存过小导致内存溢出或者耗竭的(如采用XP系统建议配置512MB内存以上或.

    2022年10月8日
    3
  • NS_TEST_ns是什么软件

    NS_TEST_ns是什么软件TEST

    2025年9月1日
    10
  • [python机器学习及实践(2)]Sklearn实现朴素贝叶斯

    [python机器学习及实践(2)]Sklearn实现朴素贝叶斯

    2022年4月3日
    46
  • 安装SQLServer2008失败「建议收藏」

    安装SQLServer2008失败「建议收藏」由于操作系统不同(64位与32位)和可能安装的环境不一样再或者在安装SQL2008的时候已经安装SQLServer相关其他版本,因此可能会遇到问题。  问题1:安装sqlserver2008R2,安装过程中提示错误:此计算机上安装了MicrosoftVisualStudio2008的早期版本。请在安装SQLServer2008前将Microsoft…

    2025年9月7日
    8
  • 【教程】2026年OpenClaw(Clawdbot)华为云5分钟搭建集成Skill保姆级流程

    【教程】2026年OpenClaw(Clawdbot)华为云5分钟搭建集成Skill保姆级流程

    2026年3月14日
    2
  • 微信公众号网页开发-Vue项目坑点分析

    微信公众号网页开发-Vue项目坑点分析项目背景该系统为商品推广系统,微信交互涉及网页授权和网页分享,年前使用django框架做了基础版,节后项目升级,功能更加丰富,使用vue框架做前后端分离开发,前后端分离开发提升了开发效率,也埋藏了很多坑点。。。坑点分析及解决开发前需要进入公众号后台配置”JS接口安全域名”及“授权回调页面域名”,如果服务端要获取token,还需要配置”IP白名单”,详细信息可以查阅官网。微信公众号官方文档:微信公众平台开发概述|微信开放文档微信开发者平台文档https://developers.weixi

    2022年6月5日
    32

发表回复

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

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