Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Python lambda和reduce函数

Python lambda和reduce函数

作者头像
全栈程序员站长
发布于 2022-01-27 00:02:30
发布于 2022-01-27 00:02:30
4160
举报

大家好,又见面了,我是全栈君。

看到一篇博文写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.cn/116036.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Python3函数完全指南:从基础到高级用法详解
Python有四种作用域:局部(Local)、闭包(Enclosing)、全局(Global)、内置(Built-in)
用户11638464
2025/08/12
1390
Python的lambda表达式、filter、map、reduce等函数的用法
参考链接: Python lambda (匿名函数) | filter, map, reduce
用户7886150
2021/01/12
1.2K0
Python基础面试,看这篇文章画重点吧
一直想写一些更加基础的文章,但是总是想不到好的点子,最近到了就业季,一大堆学生面临就业了,正好,从Python的面试题出发,分析和解答一些常见的面试题,并且总结一些文字。
py3study
2020/01/15
4880
python 几个重要函数
lambda argument1, argument2,...,argumentN: expression using arguments
py3study
2020/01/08
6570
Python之解析式您知多少?
解析式 解析式 今天给大家介绍Python中的解析式。 解析式简单介绍 解析式, 列表解析 # Python2只有列表解析 生成器解析 # Python3特有 集合解析 # Python3特有 字
1846122963
2018/03/09
1K0
Python之解析式您知多少?
Python----reduce原来是这样用的
本文介绍了如何利用reduce函数对列表进行去重和计数,以及利用reduce函数实现统计一个集合中各个元素出现的次数。
古时的风筝
2018/01/08
7970
python lambda拉姆达表达式「建议收藏」
看出来,就是一个函数,也是一个表达式。 Because a lambda function is an expression, it can be named. Therefore you could write the previous code as follows:
全栈程序员站长
2022/09/28
3130
Python装饰器详解
该段代码的缺点 1.我们期望的是直接调用主要函数,同时执行辅助函数,而并不是把辅助函数写在前面。
Yuou
2022/09/26
2910
python学习(day4)
1、装饰器: ''' 实现装饰器只是储备: 1、函数即“变量” 2、高阶函数 3、嵌套函数 高阶函数+嵌套函数=》装饰器 ''' import time def timmer(func):     def warpper(*args,**kwargs):         start_time = time.time()         func()         stop_time = time.time()         print("the func run time is %s"%(stop_t
py3study
2020/01/10
3070
Python 函数式编程到底怎么写?至少应该学会这几个函数
在第一条语句中,采用 lambda 创建了一个无参的函数 func1。这和下面采用 def创建函数的效果是相同的。
AlwaysBeta
2020/11/25
7880
Python教程(16)——lambda函数介绍
我们平时经常可以在Python的代码中看到一种lambda开头的这种表达式,如果没有学过Python的相关知识,可能会一脸懵逼,不清楚到底这个关键字是干嘛的,用来表示什么。
一点sir
2023/12/20
3381
Python 复习
函数式编程 map(function, sequence): 入参是为函数和列表/元组/字符串,返回值为function(item)列表。 filter(function, sequence): 入参为函数和列表/元组/字符串,返回值为item列表/元组/字符串。 reduce(function, sequence, starting_value): 入参是为函数和列表/元组/字符串和初始值,返回值为数值 lambda : 匿名函数 回调函数 : 函数作为参数 闭包: 绑定外部变量的函数, 惰性计算, 迭代
radaren
2018/08/28
4090
一文读懂python的map、reduce函数
这对兄弟是出现频率极高且相当实用的python函数,初学者会较难理解,看完本文你就能搞定它们喽!
派大星的数据屋
2022/04/02
5200
一文读懂python的map、reduce函数
python-内置函数(搭配lambda使用)
目录 常用的内置函数 需要注意的知识点: enumerate()函数 map()函数 zip()函数 filter()函数 reduce()函数 sum()函数 max()/ min()函数 sort()函数 sorted()函数 内置函数一览表: 常用的内置函数 学习! 需要注意的知识点: 大部分内置函数经常搭配lambda函数使用 可迭代对象:字符串、列表、元组、字典、集合、文件对象 注意!!!! 在py3中部分函数的返回值是一个迭代器,不能多次遍历,(map、filter、z
HammerZe
2022/03/25
7970
python-内置函数(搭配lambda使用)
【Python 第73课】reduce 函数
上次说了 Python 中一个比较有意思的内置函数 map,今天再来介绍另一个类似的函数:reduce map 可以看作是把一个序列根据某种规则,映射到另一个序列。reduce 做的事情就是把一个序列根据某种规则,归纳为一个输出。 上栗子。以前我们给过一个习题,求1累加到100的和。寻常的做法大概是这样: sum = 0 for i in xrange(1, 101): sum += i print sum 如果用 reduce 函数,就可以写成: lst = xrange(1, 101) def add
Crossin先生
2018/04/16
5960
Python应用之计算阶乘
当 m 是自然数时,表示不超过 m 且与 m 有相同奇偶性的所有正整数的乘积。如:
芯动大师
2022/11/15
1.8K0
Python应用之计算阶乘
Python基础:lambda 匿名函数
  匿名函数 lambda 和常规函数一样,返回的都是一个函数对象(function object)
py3study
2020/01/15
3990
部分计算机代数系统对比Computer Algebra Software: Mathematica, Maxima, Pari/GP
Computer Algebra Software: Mathematica, Maxima, Pari/GP
Enjoy233
2019/03/05
8640
Python函数进阶:探索高级函数特性与技巧
Python中的函数不仅仅是一段可重用的代码块,还具备强大的进阶特性,如函数装饰器、匿名函数、闭包、生成器、递归等。本文将深入探讨Python函数的高级特性与技巧,以帮助你更好地编写清晰、灵活和高效的代码。
海拥
2023/09/19
7020
Python函数进阶:探索高级函数特性与技巧
python lambda表达式_python表达式是什么
lambda表示的是匿名函数,不需要用def来声明,一句话就可以声明出一个函数
全栈程序员站长
2022/09/20
5590
相关推荐
Python3函数完全指南:从基础到高级用法详解
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档