Python 学习笔记

一、Python 学习


1、Python 参考资料

2、高阶函数

(1) map()

  • 用法: map(function,variable)
  • 例子:

    def f(x):
        return x*x            
    r=map(f,[1,2,3])
    ans=list(r)
    Out:[1,4,9]
    

(2) reduce()

  • 用法: from functools import reduce reduce(fun,var)
  • 例子:

    from functools import reduce
    def add(x,y):
        return x+y
    ans=reduce(add,[1,2,3])
    Out:6
    

(3) filter()

  • 用法: filter(fun, var)
  • 例子: list(filter(is_odd, [1,2,3,4,5])), Out:[1,3,5]