python函数总结

1. map(function, sequence, *sequence_1)

    function:是一个函数或者一个方法,这个函数和方法需要一个参数

    sequence:是一个列表,这个列表中的元素都是这个

    map将sequence中的元素逐个放入function中,这样function将一个一个返回执行之后的值,map将这些值生成一个新的列表

    我们可以认为,sequence经过function之后,全部变成了另一个新列表,列表的元素个数和sequence一致.

以下是python文档中的内容:

Return a list of the results of applying the function to the items of the argument sequence(s). If more than one sequence is given, the function is called with an argument list consisting of the corresponding item of each sequence, substituting None for missing values when not all sequences have the same length. If the function is None, return a list of the items of the sequence (or a list of tuples if more than one sequence).

 

2、zip  作用:可以用作并行迭代,可以把连个序列“压缩”在一起,然后返回一个元祖列表

names = ['anne','beth','george','damon']

ages = [12,45,32,102]

>>>zip(names,ages)

[('anne',12),('beth',45),('george',32),('damon',102)]

for name,age in zip(names,ages):
  print(name,'is',age,'years old')

anne is 12 years old
beth is 45 years old
george is 32 years old
damon is 102 years old

之后,我们可以在循环中解包元祖,

zip也可以用作于任意多的序列。关于它的一个很重要的知识点是zip可以处理不等长的序列,当最短的序列用完时,就会停止

 

3、filter(func,seq)

返回seq中元素其函数func为真的元素形成的新列表

 

4、reduce(func,seq【,initial】)

等同于func(func(func(seq[0],seq[1]),seq[2]),....)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值