Python - map函数

1. 函数原型

help(map)一下:

map(func, *iterables) -> map object;Make an iterator that computes the function using arguments from each of the iterables.  Stops when the shortest iterable is exhausted. 将可迭代对象中的每一个元素经过函数func,最终返回一个迭代器;支持并行,同时对多个iterable进行转化,当最短的iterable被转化完毕后,程序结束。


2. 应用

2.1 单个可迭代对象

# 1. 单个可迭代对象
for i in map(lambda x: 2*x, range(7)):
    print(i)

def upper(char):
    return str.upper(char)

string = "akadfdkaf"
for i in map(upper, string):
    print(i)


2.2 并行用于多个可迭代对象

# 2. 多个可迭代对象
for i in map(lambda x, y: x*y, range(7), range(10,17)):
    print(i)
# 2. 多个可迭代对象
for i,j in map(lambda x, y: (x+y, x*y), range(7), range(10,17)):
    print(i,j)

# 并行
for i in map(lambda x,y: x + y, [1, 1, 1, 1], [0, 1, 0]):
    print(i)
    
"""
1 2 1
"""


2.3 类型转换

适用于像列表中又包含列表这样的转换,因此此时不能直接用set([[]])将其中每一个列表转化为集合

# 列表转集合
for i in map(set, [[1, 2, 3, 1, 4], [4, 4, 4]]):
    print(i)

"""
{1, 2, 3, 4}
{4}
"""


参考文献

http://blog.csdn.net/seetheworld518/article/details/46959871

http://www.cnblogs.com/superxuezhazha/p/5714970.html

https://my.oschina.net/zyzzy/blog/115096

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值