美文网首页python整理
python函数:map()

python函数:map()

作者: 随风化作雨 | 来源:发表于2017-05-22 15:31 被阅读18次

    描述:
    map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回。

    实例:

    a = map(lambda x:x*x,[0,1,2,3,4,5,6,7,8,9])
    print a
    

    如果你熟悉列表解析的话,可以写成这样:

    a = map(lambda x:x*x,[i for i in range(10)])
    print a
    

    得到的结果是:

    [0,1, 4, 9, 16, 25, 36, 49, 64, 81]
    

    相关文章

      网友评论

        本文标题:python函数:map()

        本文链接:https://www.haomeiwen.com/subject/vezbxxtx.html