举例说明

#例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. 向现有mvc程序中加入devexpress report

    Open your ASP.NET MVC project. In the main menu of Visual Studio, click the DEVEXPRESS submenu and s ...

  2. Jenkins内存溢出的处理方法

    参考:http://openwares.net/java/jenkens_deploy_to_tomcat_error_of_outofmemoryerror.html上的说明,有如下解释: -Xms ...

  3. React 入门之路

    React React简介 是由Facebook公司推广的一套框架,已经应用instagram等产品 React就是为了提供应用程序性能而设计的一套框架 在angular中,对dom提供了一些指令,让 ...

  4. 聊聊、Zookeeper Linux 单服务

    关于上一篇 Zookeeper 的文章是介绍安装启动,这一篇介绍独立服务,也就是单台 Zookeeper 提供服务.首先登陆 Linux 系统,确保网络通畅.如果遇到找不到网卡 eth0 情况,可以先 ...

  5. Android 使用SharedPreferences数据存储

    自己写了个SP辅助类 尽管写的有点啰嗦,也是自己的成果.例如以下: package com.yqy.yqy_testsputil; import android.annotation.Suppress ...

  6. ZOJ - 3228 Searching the String (AC自己主动机)

    Description Little jay really hates to deal with string. But moondy likes it very much, and she's so ...

  7. windows 屏幕坐标 窗口坐标 客户区坐标 逻辑坐标 设备坐标之间的关系及转换

    设置坐标映射    (1)Windows坐标系统 Windows坐标系分为逻辑坐标系和设备坐标系两种,GDI支持这两种坐标系.一般而言, GDI的文本和图形输出函数使用逻辑坐标,而在客户区移动或按下鼠 ...

  8. selenium用java找到表格某一行某一列中含有特定文字的某个元素

    html部分代码如下: <tbody> <tr class="odd"> <td>1609</td> <td>-YOUK ...

  9. Redis 在 Java 中的使用

    转:http://blog.csdn.net/jiangtao_st/article/details/8256610 一.下载jar包 https://github.com/xetorthio/jed ...

  10. 强制重启Linux系统的几种方法

    实际生产环境中某些情况下 Linux 服务器系统在出现致命错误需要远程进行重启,通过常规的 reboot.init 6 等方法无法正常重启(例如重启时卡在驱动程序里等情况),这时就需要通过下面介绍的几 ...