map函数

作者: 秋山琳 | 来源:发表于2018-12-03 16:47 被阅读0次

    map会将一个函数映射到一个输入列表的所有元素上,如:

    items = [1,2,3,4,5]

    squared = []

    for i in items:

        squared.append(i**2)

    return squared

    map可以让我们用一种简单而漂亮的方式实现:

    items = [1,2,3,4,5]

    squared = list(map(lambda x : x**2, items))

    map不仅可以用于一列表的简单元素输入,还可以用于一列表的函数输入:

    def add(x):

        return (x+x)

    def multiply(x):

        return (x*x)

    funcs = [add,multiply]

    for i in range(5):

        value = list(map(lambda x : x (i), funcs))

        print(value)

    相关文章

      网友评论

          本文标题:map函数

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