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
时间: 2024-10-24 18:20:59