Python中如何使用map函数

发布时间:2021-07-14 16:11:59 作者:Leah
来源:亿速云 阅读:222

Python中如何使用map函数,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。


用法

  1. map(func, seq1[, seq2,…])

map接收两个参数,第一个参数是函数名,第二个是一个或多个可迭代的序列,返回的是一个集合。运行时,map()将func作用于序列中的每一个元素,并将结果作为一个list返回。如果func为None,作用同zip()。
2.1 当seq 只有一个时,map函数返回将func函数作用于 seq每个元素并返回一个新的list集合,
Python中如何使用map函数
比如需要将seq的元素乘以2 ,使用map可以写成

  1. In [4]: l=[1, 2, 3, 4, 5, 6, 7, 8, 9]

  2. In [5]: map(lambda x:x*2 ,l)

  3. Out[5]: [2, 4, 6, 8, 10, 12, 14, 16, 18]

如果使用函数来做:

  1. rest=[]

  2. for i in l:

  3.   rest.append(i*2)

  4. print rest

map作为高阶函数,能够把运算规则抽象化,显然map 更精简。

2.2 当有多个seq时,map可以并行的取每个seq对应的第M个元素 seqN[M],进行计算。
Python中如何使用map函数
例子:

  1. In [9]: map(lambda x , y : x * y, [2,4,6],[3,2,1])

  2. Out[9]: [6, 8, 6]

记得多个seq的元素个数必须一致 不然会报错 

  1. In [1]: print map(lambda x , y : x ** y, [2,4,6],[3,2,1])

  2. [8, 16, 6]

  3. seq1=[2,4,6] ,seq2=[3,2] 元素个数不一致则报错。

  4. In [3]: print map(lambda x , y : x ** y, [2,4,6],[3,2])

  5. ---------------------------------------------------------------------------

  6. TypeError                                 Traceback (most recent call last)

  7. <ipython-input-3-d075c46afd0b> in <module>()

  8. ----> 1 print map(lambda x , y : x ** y, [2,4,6],[3,2])

  9. <ipython-input-3-d075c46afd0b> in <lambda>(x, y)

  10. ----> 1 print map(lambda x , y : x ** y, [2,4,6],[3,2])

  11. TypeError: unsupported operand type(s) for ** or pow(): 'int' and 'NoneType'

  12. In [4]:

2.3 map在多进程中的应用。

  1. #!/usr/bin/env python

  2. # encoding: utf-8

  3. """

  4. author: yangyi@youzan.com

  5. time: 2017/7/16 上午10:42

  6. func: 参考网络上的例子 

  7. """

  8. import urllib2

  9. from multiprocessing.dummy import Pool as ThreadPool

  10. urls = [

  11.         'http://www.python.org',

  12.         'http://www.python.org/about/',

  13.         'http://www.onlamp.com/pub/a/python/2003/04/17/metaclasses.html',

  14.         'http://www.python.org/doc/',

  15.         'http://www.python.org/download/',

  16.         'http://www.python.org/getit/',

  17.         'http://www.python.org/community/',

  18.         'https://wiki.python.org/moin/',

  19.         'http://planet.python.org/',

  20.         'https://wiki.python.org/moin/LocalUserGroups',

  21.         'http://www.python.org/psf/',

  22.         'http://docs.python.org/devguide/',

  23.         'http://www.python.org/community/awards/'

  24.         ]


  25. # Make the Pool of workers

  26. pool = ThreadPool(4)

  27. # Open the urls in their own threads

  28. # and return the results

  29. results = pool.map(urllib2.urlopen, urls)

  30. #close the pool and wait for the work to finish

  31. pool.close()

  32. pool.join()

关于Python中如何使用map函数问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

推荐阅读:
  1. python如何使用map、reduce函数
  2. python中map()函数语法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python map

上一篇:Python 中如何使用set函数

下一篇:JDBC连接数据库的方法有哪些

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》