高阶函数,map,filter,sorted

Map:对列表中的每个元素进行操作

>>> def f(x):
...     return x * x
...
>>> map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9])
[1, 4, 9, 16, 25, 36, 49, 64, 81]

filter:对列表中都每个元素进行筛选

def is_odd(n):
    return n % 2 == 1

filter(is_odd, [1, 2, 4, 5, 6, 9, 10, 15])
# 结果: [1, 5, 9, 15]

sorted:对列表进行排序

>>> sorted([36, 5, 12, 9, 21])
[5, 9, 12, 21, 36]

 





posted @ 2016-08-08 16:05  意发并行  阅读(134)  评论(0编辑  收藏  举报