举例说明

#例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. Css 基础学习

    css 基本选择器 css基本选择器有三种方法 第一种: class选择器 .c1{ color: red;} <div class='c1'>hello world</div> ...

  2. Enter Query Mode Search Tricks Using Enter_Query Built-in in Oracle Forms

    In this post you will learn how to specify any condition in enter query mode of Oracle Forms. Whenev ...

  3. zabbix-agent安装报错

    最近接触了zabbix,觉得挺好用的,再一次安装agent的过程中,报了如下错误: [root@11005499 ~]# yum install zabbix-agent -y ... groupad ...

  4. 黑苹果+win10双系统折腾笔记

    寒假趁机在家折腾一下黑苹果 笔记本配置:神船K610D I7 4600 ,其他配置思路一样,驱动要自己找 镜像和工具:OS X Yosemite 10.10.3 镜像 WIN10 TLSB 2016 ...

  5. 给交换机端口设ip

    先给端口设vlan,再给vlan设ip [H3C]vlan [H3C-vlan100]port GigabitEthernet // <H3C>sy System View: return ...

  6. android listView 滑动载入数据 该数据是服务端获取的

    package com.sunway.works.applycash; import java.util.ArrayList; import java.util.Calendar; import ja ...

  7. Log4cplus入门

    Log4cplus使用指南 1.  Log4cplus简单介绍 log4cplus是C++编写的开源的日志系统,前身是java编写的log4j系统.受Apache Software License保护 ...

  8. 有问必答项目 -数据库设计文档(ask-utf-8)

    有问必答项目 -数据库设计文档(ask-utf-8) 表前缀的使用 早期租用公共的服务器 一个数据库,保存多个项目(问答.电子商务.医院),为了区分这些项目,使用前缀分割 ask_ ec_ hospi ...

  9. MFC中函数的使用

    函数语句: ((CStatic*)GetDlgItem(IDC_STATIC1))->SetIcon(AfxGetApp()->LoadIconW(IDI_CLOSE)); 解释: 1.G ...

  10. MongoDB 快速入门

    本作品由Man_华创作,采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可.基于http://www.cnblogs.com/manhua/上的作品创作. MongoDB No ...