码迷,mamicode.com
首页 > 编程语言 > 详细

python map函数

时间:2018-11-11 12:51:01      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:列表   items   ems   style   square   传递   return   app   []   

 例一

?多数时候,我们要把列表中所有元素?个个地传递给?个函数,并收集输出。??说:
items = [1, 2, 3, 4, 5]
squared = []
for i in items:
    squared.append(i**2)

Map可以让我们??种简单?漂亮得多的?式来实现。就是这样: items
= [1, 2, 3, 4, 5] squared = list(map(lambda x: x**2, items))

 

例二

def multiply(x):
  return (x*x)
def add(x):
  return (x+x)
funcs = [multiply, add]
for i in range(5):
  value = list(map(lambda x: x(i), funcs))
  print(value)

输出:

[0, 0]
[1, 2]
[4, 4]
[9, 6]
[16, 8]

 

python map函数

标签:列表   items   ems   style   square   传递   return   app   []   

原文地址:https://www.cnblogs.com/sea-stream/p/9941744.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!