举例说明

#例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. java程序容错

    程序最怕出错的方式就是直接闪退 编程应该以这种方式进行,保证结构不出错,数据可容错的方式 比如 fungetsonmfrominternet(){变量 a a=从网络返回数据 return a } 在 ...

  2. dedecms 调取当前栏目的链接和 栏目名称

    <a href="{dede:field name='typeurl' function=”GetTypeName(@me)”/}" target="_blank& ...

  3. sublime去除空白行和重复行

    去除空白行 edit -> line -> delete blank lines 去除重复行 打开正则模式 1 edit-> sort lines 2 command+option+ ...

  4. circular-array-loop(蛮难的)

    https://leetcode.com/problems/circular-array-loop/ 题目蛮难的,有一些坑. 前后两个指针追赶找环的方法,基本可以归结为一种定式.可以多总结. pack ...

  5. todo提纲

    deep&wide为啥work,如何优化特征:详述attention,attention在ctr预估中如何使用,din为啥work?详述transformer,如何应用于ctr预估;item2 ...

  6. Python--图像处理(2)

    skimage提供了io模块,顾名思义,这个模块是用来图片输入输出操作的.为了方便练习,也提供一个data模块,里面嵌套了一些示例图片,我们可以直接使用. 引入skimage模块可用: 1 from  ...

  7. API测试工具postman使用总结

    一.Postman介绍: Postman是google开发的一款功能强大的网页调试与发送网页HTTP请求,并能运行测试用例的的Chrome插件,主要用于模拟网络请求包,快速创建请求,回放.管理请求,验 ...

  8. AutoCAD如何将dwf转成dwg格式

    dwf转成dwg怎么转, 悬赏分:30 - 解决时间:2009-11-22 10:19 重金:dwf转成dwg怎么转, 我是用在出图上的. 最佳答案 Design Web Format (DWF) 文 ...

  9. mongodb springdata 问题处理

    发现spring4-mongo3.2.1 加上用户名密码认证后无法认证的问题. 1.必须在当前使用的数据库下建用户,权限可以给readWrite 2.由于mongodb2和mongodb3的用户认证方 ...

  10. C# 读取Excel中的数据

    #region 读取Excel中的数据 /// <summary> /// 读取Excel中的数据 /// </summary> /// <param name=&quo ...