举例说明

#例1:
###递归函数求和
from traitlets.traitlets import Instance
def mysum(L):
print(L)
if not L:
return 0
else:
return L[0] + mysum(L[1:]) #调用自己 call myself sum1 = mysum([1,2,3,4])
print(sum1) # 编写替代方案
def mysum1(L):
return 0 if not L else L[0] + mysum1(L[1:])
print('#改写方案1')
sum1 = mysum1([1,2,3,4,5,6,7])
print(sum1) #改写方案2
def mysum2(L):
return L[0] if len(L) ==1 else L[0] + mysum2(L[1:])
sum2 = mysum2([1,2,3])
print('改写方案2')
print(sum2) #改写方案3
def mysum3(L):
first,*rest = L
return first if not rest else first + mysum3(rest) ##python3 扩展序列 ext seq
sum3 = mysum3([1,2,3,4,5])
print('改写方案3')
print(sum3)
### 用while 循环实现
L = [1,2,3,4,5]
sum2 = 0
while L:
sum2 += L[0]
L = L[1:]
print('用while 循环实现')
print(sum2) ##用for 循环实现
L = [1,2,3,4,5,6]
sum3 =0
for x in L:
sum3 += x
print('用for循环实现')
print(sum3) # 处理任意结构 def sumtree(L):
tot = 0
for x in L:
if not isinstance(x,list):
tot += x
else:
tot +=sumtree(L[1:])
return tot
L = [1,[2,[3,4],5],6,[7,8]]
print('##任意结构')
print(sumtree(L)) ##间接函数调用
def echo(message):
print(message) def indirect(func,args):
func(args)
indirect(echo, 'shixingwen') schedule1=[ (echo,'Spam!'),(echo,'Ham!') ]
for (func,args) in schedule1:
func(args) print('##间接调用函数')
print(echo.__name__) ###python 函数注解 def func(a:'spam',b:(1,3),c:float):
return a + b+c
print('##函数注释')
print(func(1,2,3))
zhushi = func.__annotations__ print(zhushi,'\n')
for args in zhushi:
print(args ,'=>', zhushi[args]) ##map 在序列中映射函数
counter = [1,2,3,4]
update = []
for i in counter:
update.append(i+10)
print('##for循环实现')
print(update) def inc(i):return i +10
print('##map 在序列中映射函数')
print(list(map(inc,counter)))
print('####lambda 也能实现')
print(list(map(lambda i:i +10,counter))) ##
from functools import reduce
re=reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
print(re) import functools
print('查询reduce用法')
help(functools.reduce) ##或者
from functools import reduce
help(reduce)

上述结果如下

[1, 2, 3, 4]
[2, 3, 4]
[3, 4]
[4]
[]
10
#改写方案1
28
改写方案2
6
改写方案3
15
用while 循环实现
15
用for循环实现
21
##任意结构
43
shixingwen
Spam!
Ham!
##间接调用函数
echo
##函数注释
6
{'c': <class 'float'>, 'a': 'spam', 'b': (1, 3)} c => <class 'float'>
a => spam
b => (1, 3)
##for循环实现
[11, 12, 13, 14]
##map 在序列中映射函数
[11, 12, 13, 14]
####lambda 也能实现
[11, 12, 13, 14]
15
查询reduce用法
Help on built-in function reduce in module _functools: reduce(...)
reduce(function, sequence[, initial]) -> value Apply a function of two arguments cumulatively to the items of a sequence,
from left to right, so as to reduce the sequence to a single value.
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5). If initial is present, it is placed before the items
of the sequence in the calculation, and serves as a default when the
sequence is empty. Help on built-in function reduce in module _functools: reduce(...)
reduce(function, sequence[, initial]) -> value Apply a function of two arguments cumulatively to the items of a sequence,
from left to right, so as to reduce the sequence to a single value.
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5). If initial is present, it is placed before the items
of the sequence in the calculation, and serves as a default when the
sequence is empty.

python 函数中的递归、lambda 、map reduce 等详解的更多相关文章

  1. python 函数及变量作用域及装饰器decorator @详解

    一.函数及变量的作用   在python程序中,函数都会创建一个新的作用域,又称为命名空间,当函数遇到变量时,Python就会到该函数的命名空间来寻找变量,因为Python一切都是对象,而在命名空间中 ...

  2. hadoop学习WordCount+Block+Split+Shuffle+Map+Reduce技术详解

    转自:http://blog.csdn.net/yczws1/article/details/21899007 纯干货:通过WourdCount程序示例:详细讲解MapReduce之Block+Spl ...

  3. Java8中聚合操作collect、reduce方法详解

    Stream的基本概念 Stream和集合的区别: Stream不会自己存储元素.元素储存在底层集合或者根据需要产生.Stream操作符不会改变源对象.相反,它会返回一个持有结果的新的Stream.3 ...

  4. 【转】 深入main函数中的参数argc,argv的使用详解

    C/C++语言中的main函数,经常带有参数argc,argv,如下: 复制代码 代码如下: int main(int argc, char** argv) 这两个参数的作用是什么呢?argc 是指命 ...

  5. python 函数式编程之lambda( ), map( ), reduce( ), filter( )

    lambda( ), map( ), reduce( ), filter( ) 1. lambda( )主要用于“行内函数”: f = lambda x : x + 2 #定义函数f(x)=x+2 g ...

  6. Python 函数中,参数是传值,还是传引用?

    在 C/C++ 中,传值和传引用是函数参数传递的两种方式,在Python中参数是如何传递的?回答这个问题前,不如先来看两段代码. 代码段1: def foo(arg): arg = 2 print(a ...

  7. python 函数中使用全局变量

    python 函数中如果需要使用全局变量,需要使用 global + 变量名 进行声明, 如果不声明,那么就是重新定义一个局部变量,并不会改变全局变量的值 n [1]: a = 3 In [2]: d ...

  8. Python函数中的可变参数

    在Python函数中,还可以定义可变参数. 如:给定一组数字a,b,c……,请计算a2 + b2 + c2 + ……. 要定义出这个函数,我们必须确定输入的参数.由于参数个数不确定,我们首先想到可以把 ...

  9. python函数中把列表(list)当参数时的"入坑"与"出坑"

    在Python函数中,传递的参数如果默认有一个为 列表(list),那么就要注意了,此处有坑!! 入坑 def f(x,li=[]): for i in range(x): li.append(i*i ...

随机推荐

  1. 微信工作汇报系统2——IOS原型设计

    上一篇博客:一款自动汇报工作的微信机器人 上一篇博客中说道,我打算自己做一款能自动汇报工作的微信机器人,可惜学识有限,最后不知道怎么实现让机器人学习我的文本说话,所以就一直耽搁了,见天又打开这个系列, ...

  2. getenv, _wgetenv

    Description The C library function char *getenv(const char *name) searches for the environment strin ...

  3. UIView和UIImageView 旋转消除锯齿方法

    方法一: calendarImageView_ =[[UIImageView alloc] initWithFrame:CGRectMake(3,3,60,72)];     calendarImag ...

  4. 【mob】Android短信验证+源码

    在很多的应用当中,都涉及到了短信验证的功能,比如在注册或者找回密码的时候,那么我们如何通过第三方的平台来完成这个功能呢? 本面博文就实现短信验证,来做一个小的栗子. 第一步-下载开发包 第二步-将SD ...

  5. dedecms 留言板中引用模板文件方法

    最近在做一个用dedecms搭建的网站,客户提出要有留言板,dedecms带了一个留言板的模块,安装倒是十分简便,但装完后发现界面十分粗糙.装修比较简单,但是发现遇到一个问题:网站通用的导航栏无法显示 ...

  6. 第一次用THINKPHP 报路径错

    我第一次 看网上写的代码 define('THINK_PATH','ThinkPHP');define('App_NAME','43');define('App_PATH','.'); require ...

  7. GlusterFS源代码解析 —— GlusterFS 内存分配方式

    原文地址:http://blog.csdn.net/wangyuling1234567890/article/details/24564891 GlusterFS 的内存分配主要有两种方式,一种是内存 ...

  8. scramble-string——两个字符串经过树化并旋转后是否一致、递归、动态规划

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  9. 登陆界面综合实例+spinner下拉列表框详解

      点击确定: 点击修改:     想到Spinner下拉按钮可以通过两种方法生成 方法一: 1.在array数组里面定义数组 代码如下: <resources> <st <? ...

  10. Effective C++ 35,36,37

    35.使公有继承体现 "是一个" 的含义. 共同拥有继承意味着 "是一个".如  class B:public A. 说明类型B的每个对象都是一个类型A的对象, ...