码迷,mamicode.com
首页 > 其他好文 > 详细

内置函数起始值

时间:2015-09-16 09:45:00      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

ref:

http://mp.weixin.qq.com/s?__biz=MjM5NzU0MzU0Nw==&mid=211637608&idx=1&sn=1aa3d81c2c56fc87533c49f912692742&scene=0&key=dffc561732c22651a58840064393fadfff612048d19d8fde33d3062e3072d56b15a4c6f3346a0b8fcd142ef0607dbf92&ascene=1&uin=MjI2MTM3MDUwMA%3D%3D&devicetype=webwx&version=70000001&pass_ticket=Z7bdKMF60ppUTY3aii%2BXlJ2hbVAiWlXhZJOa22Y5qV8ugvvf%2BiChYRBPEyxp3KSq

第一个就蒙了,reduce怎么出来这么多参数,尤其最后一个不理解

后来试了下:

>>> reduce(lambda x,y:x+y,range(101),1)
5051

1应该是个起始值,但后面紧跟着的操作,是把range产生的 0赋值给y继续,还是赋值给x继续,不清楚

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.

 

到目前为止还见过有起始值的enumerate

t=range(10)
>>> for index,value in enumerate(t,start=1):
    print index,value

    
1 0
2 1
3 2
4 3
5 4
6 5
7 6
8 7
9 8
10 9

 以及sum

>>> sum(range(0,101),1)
5051

 

内置函数起始值

标签:

原文地址:http://www.cnblogs.com/Citizen/p/4812232.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!