023_Python3 map 函数高级用法

# -*- coding: UTF-8 -*-

'''
map(function, iterable, …)
    function -- 函数
    iterable -- 一个或多个序列

返回值
    Python 2.x 返回列表。
    Python 3.x 返回迭代器。
'''

# 1.1 简单用法
lst = ['1', '2', '3']
lst1 = list(map(int, lst))
print(lst1)  # [1, 2, 3]


def add(x):
    return x + 100


lst = [1, 2, 3]
lst1 = list(map(add, lst))
print(lst1)  # [101, 102, 103]


# 1.2 多个可迭代参数
def add3(x, y, z):
    return x * 10 + y * 10 + z


lst = [1, 2, 3]
lst1 = [1, 2, 3, 4]
lst2 = [1, 2, 3, 4, 5]
lst3 = list(map(add3, lst, lst1, lst2))
print(lst3)  # [21, 42, 63]

# 1.3 与 lambda
lst = [1, 2, 3]
lst1 = list(map(lambda x: x * x, lst))
print(lst1)  # [1, 4, 9]

lst2 = list(map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10]))
print(lst2)  # 3, 7, 11, 15, 19]
原文地址:https://www.cnblogs.com/luwei0915/p/14612091.html