python函数式编程map

#python函数式编程之map用法

map(f,iterator)把结果作为新的iterator返回
map返回什么结果看iterator是什么类型

  1. example 1

    1
    2
    3
    4
    5
    def f(x):
    return x*x
    ans = map(f,[1,2,3,4,5,6,7,8,9])#f作用在list的每一个元素并把结果返回为新的list
    print(ans)#看不懂的东西
    print(list(ans))# iterator是一个惰性序列,通过list让整个函数都计算出来并返回一个list
  2. example 2

    1
    2
    ans = list(map(str,[1,2,3,4,5,6,7,8,9]))
    print(ans)