|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 ~风介~ 于 2017-3-11 00:04 编辑
方法:
- | map(func, *iterables) --> map object
- |
- | Make an iterator that computes the function using arguments from
- | each of the iterables. Stops when the shortest iterable is exhausted.
复制代码 代码:#Python3.4.0
- >>> def f(x):
- return x*x
- >>> list(map(f,[1,2,3,4,5,6]))
- [1, 4, 9, 16, 25, 36]
- >>>
复制代码 -- >>> def ff(x, y):
- return x+y
- >>> list(map(ff, [1,2,3], [4,5,6]))
- [5, 7, 9]
复制代码
******************************
方法
- 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.
复制代码 翻译:
- reduce函数即为化简,它是这样一个过程:每次迭代,将上一次的迭代结果(第一次时为init的元素,如没有init则为seq的第一个元素)与下一个元素一同执行一个二元的func函数。在reduce函数中,init是可选的,如果使用,则作为第一次迭代的第一个元素使用。
复制代码
图例:
代码:#Python3.4.0
- >>> from functools import reduce
- >>> def ad(x,y):
- return x + y
- >>> reduce(ad, [1, 3, 5, 7, 9])
- 25
- >>>ad(ad(ad(ad(1,3),5),7),9)
- 25
复制代码 -求阶乘- >>> from functools import reduce
- >>> reduce(lambda x, y: x * y, range(1, 5 + 1)) #1*2*3*4*5
- 120
- >>>reduce(lambda x, y: x * y, range(1, 5 + 1), 2)#2*1*2*3*4*5
- 240
复制代码
参考:廖雪峰的官方网站
|
评分
-
查看全部评分
|