[Built-in Functions] - M
map(function, sequence)
对sequence中的item依次执行function(item),执行结果组成一个List返回。
>>> def cube(x): return x*x*x
>>> map(cube, range(1, 11))
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
>>> def cube(x) : return x + x
>>> map(cube , "abcde")
['aa', 'bb', 'cc', 'dd', 'ee']
# 另外map也支持多个sequence,这就要求function也支持相应数量的参数输入:
>>> def add(x, y): return x+y
>>> map(add, range(8), range(8))
[0, 2, 4, 6, 8, 10, 12, 14]
max(iterable[, key])
max(arg1, arg2, *args[, key])
返回给定参数中的最大值,参数可以为序列。
如果指定了key,则按照key指定的函数的规则返回最大值。
node_list = ['a.b.c.d11u.e.f.g', 'e.f88.g', 'caa3.z.brr', 'z.48.ff.ee']
max_node = max(node_list, key=lambda n: n.count('.'))
在这里,使用key参数改变了max比较列表元素的方法。
min(iterable[, key])
min(arg1, arg2, *args[, key])
与max类似。