函数2-python

首先看一下函数的递归调用(recursion)

看两个经典的例子:一个是计算阶乘的,一个是计算乘方的

1.

>>> deffactorial(n):

      if n == 1:

             return 1

      else:

             return n*factorial(n-1)

 

      

>>>factorial(10)

3628800

第二个:

>>> defpower(x,n):

      if n==0:

             return 1

      else:

             return x*power(x,n-1)

 

      

>>> power(2,4)

16

>>> 

 

 

下面介绍三个个有用的内建函数:

1 map()

Map函数是通过执行一个函数,把一个序列变成另外一个序列。它的返回值是一个列表list

可以看一下它的help信息:

map(...)

   map(function, sequence[, sequence, ...]) -> list

   

    Return alist of the results of applying the function to the items of

    theargument sequence(s).  If more than one sequenceis given, the

    functionis called with an argument list consisting of the corresponding

    item ofeach sequence, substituting None for missing values when notall

    sequenceshave the same length.  If the function is None,return a list of

    the itemsof the sequence (or a list of tuples if more than onesequence).

下面看几个列子:

>>>map(ord,'Hello,World')

[72, 101, 108, 108, 111, 44, 87, 111, 114, 108, 100]

>>>map(chr,[72,101,108,108,111,44,87,111,114,108,100])

['H', 'e', 'l', 'l', 'o', ',', 'W', 'o', 'r', 'l', 'd']

>>> 

 

2 filter()

Filter函数就是一个滤波器,它可以把一个序列中我们不想的元素滤掉,然后返回一个新的list,tuple,orstring。看help的信息。

filter(...)

   filter(function or None, sequence) -> list, tuple,or string

   

    Returnthose items of sequence for which function(item) istrue.  If

    functionis None, return the items that are true.  Ifsequence is a tuple

    orstring, return the same type, else return a list.

看例子:
>>>numbers=map(ord,'Hello,World')

>>> numbers

[72, 101, 108, 108, 111, 44, 87, 111, 114, 108, 100]

>>> filter(lambdan:n%2= =0,numbers)

[72, 108, 108, 44, 114, 108, 100]

这里是把偶数给过滤出来了。这里用到了lambda表达式。他是一个简单的函数,;lambda是keyword,n是参数,然后一个冒号后跟一个表达式,整个函数就是返回这个表达式的值。

 

其实map和filter的功能都可以由list自己来实现,看例子:
>>>numbers=map(ord,'Hello,World')

>>> [chr(n) for nin numbers]

['H', 'e', 'l', 'l', 'o', ',', 'W', 'o', 'r', 'l', 'd']

>>> [ord(c) for cin 'Hello,World']

[72, 101, 108, 108, 111, 44, 87, 111, 114, 108, 100]

>>> [n for n innumbers if n%2= =0]

[72, 108, 108, 44, 114, 108, 100]

>>> 

但是一点,内建函数map和filter处理的速度是要快一点的。

 

3 reduce()

Reduce是处理列表里面的元素的,根据给定的函数先处理前两个元素,生成的新元素在和第三个元素一起处理,以此类推。直至生成最后一个数。

reduce(...)

   reduce(function, sequence[, initial]) -> value

   

    Apply afunction of two arguments cumulatively to the items of asequence,

    from leftto right, so as to reduce the sequence to a single value.

    Forexample, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates

   ((((1+2)+3)+4)+5).  If initial is present, it isplaced before the items

    of thesequence in the calculation, and serves as a default when the

    sequenceis empty.

例子:
>>> reduce(lambdax,y:x+y,[1,2,3,4,5,6])

21

实现的过程其实就(((((1+2)+3)+4)+5)+6)

其实就是相当于sum()函数

>>>sum([1,2,3,4,5,6])

21

>>> 

 

函数是相当灵活的,只有用了才知道。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值