zoukankan      html  css  js  c++  java
  • 学习python -- 第022天 map函数

    map()是一个 Python 内建函数,它允许你不需要使用循环就可以编写简洁的代码。

    一、Python map() 函数

    这个map()函数采用以下形式:

    map(function, iterable, ...)

    它需要两个必须的参数:

    • function - 针对每一个迭代调用的函数
    • iterable - 支持迭代的一个或者多个对象。在 Python 中大部分内建对象,例如 lists, dictionaries, 和 tuples 都是可迭代的。

    在 Python 3 中,map()返回一个与传入可迭代对象大小一样的 map 对象。在 Python 2中,这个函数返回一个列表 list。

    让我们看看一个例子,更好地解释map()函数如何运作的。假如我们有一个字符串列表,我们想要将每一个元素都转换成大写字母。

    想要实现这个目的的一种方式是,使用传统的for循环:

    directions = ["north", "east", "south", "west"]
    directions_upper = []
     
    for direction in directions:
      d = direction.upper()
      directions_upper.append(d)
     
    print(directions_upper)

    输出:

    ['NORTH', 'EAST', 'SOUTH', 'WEST'

    使用 map() 函数,代码将会非常简单,非常灵活。

    def to_upper_case(s):
      return s.upper()
     
    directions = ["north", "east", "south", "west"]
     
    directions_upper = map(to_upper_case, directions)
     
    print(list(directions_upper))

    我们将会使用list()函数,来将返回的 map 转换成 list 列表:

    输出:

    ['NORTH', 'EAST', 'SOUTH', 'WEST']

    如果返回函数很简单,更 Python 的方式是使用 lambda 函数:

    directions = ["north", "east", "south", "west"]
     
    directions_upper = map(lambda s: s.upper(), directions)
     
    print(list(directions_upper))

    一个 lambda 函数是一个小的匿名函数。
    下面是另外一个例子,显示如何创建一个列表,从1到10。

    squares = map(lambda n: n*n , range(1, 11))
    print(list(squares))

    输出:

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

    `range()` 函数生成一系列的整数。

    二、对多个迭代对象使用map()

    你可以将任意多的可迭代对象传递给map()函数。回调函数接受的必填输入参数的数量,必须和可迭代对象的数量一致。

    下面的例子显示如何在两个列表上执行元素级别的操作:

    def multiply(x, y):
      return x * y
     
    a = [1, 4, 6]
    b = [2, 3, 5]
     
    result = map(multiply, a, b)
     
    print(list(result))

    输出:

    [2, 12, 30]

    同样的代码,使用 lambda 函数,会像这样:

    a = [1, 4, 6]
    b = [2, 3, 5]
     
    result = map(lambda x, y: x*y, a, b)
     
    print(list(result))

    当提供多个可迭代对象时,返回对象的数量大小和最短的迭代对象的数量一致。

    让我们看看一个例子,当可迭代对象的长度不一致时:

    a = [1, 4, 6]
    b = [2, 3, 5, 7, 8]
     
    result = map(lambda x, y: x*y, a, b)
     
    print(list(result))

    超过的元素 (7 和 8 )被忽略了。

    [2, 12, 30]

    三、简单案例

    案例一# 计算平方数
    def square(x):
    return x * xobj = map(square, [1, 2, 3])
    print(type(obj), obj)
    print(list(obj))

    ## 输出
    # <class 'map'> <map object at 0x0000023BC9B59D88>
    # [1, 4, 9]

    案例二# 使用 lambda 匿名函数计算平方数
    square = map(lambda x: x ** 2, [1, 2, 3, 4, 5])
    print(square, list(square))

    ## 输出
    # <map object at 0x0000015705389D88>
    # [1, 4, 9, 16, 25]


    案例三# 按首字母大写,后字母小写规则显示名字
    name_list = ['chengzi', 'JACK', 'wangLi']
    def format_name(name_list):
    return name_list[0:1].upper()+name_list[1:].lower()obj = map(format_name, name_list)
    print(obj, list(obj))

    ## 输出
    # <map object at 0x000001FCF0D76708>
    # ['Chengzi', 'Jack', 'Wangli']


    四、总结

    Python 的 map()函数作用于一个可迭代对象,使用一个函数,并且将函数应用于这个可迭代对象的每一个元素。

    认清现实,放弃幻想。 细节决定成败,心态放好,认真学习与工作。
  • 相关阅读:
    Codeforces Round #260 (Div. 1) A
    cdoj 1131 男神的礼物 区间dp
    bzoj2435: [Noi2011]道路修建 树上dp
    hdu 4597 Play Game 区间dp
    cdoj 31 饭卡(card) 01背包
    hdu5256 序列变换 dp LIS
    BZOJ 4027: [HEOI2015]兔子与樱花 树上dp
    Codeforces Round #202 (Div. 1) D. Turtles dp
    hdu 4114 Disney's FastPass 状压dp
    Python help() 函数
  • 原文地址:https://www.cnblogs.com/jyf2018/p/15346645.html
Copyright © 2011-2022 走看看