首页 > 编程语言 > 详细

[Python] map Method

时间:2017-03-23 21:27:06      阅读:140      评论:0      收藏:0      [点我收藏+]

Map

applies a function to all the items in an input_list

 

Blueprint

map(function, list_of_inputs)

  

Most of the times we want to pass all the list elements to a function one-by-one and then collect the output. For instance:

items = [1, 2, 3, 4, 5]
squared = []
for i in items:
    squared.append(i**2)

  

Map allows us to implement this in a much simpler and nicer way. Here you go:

 
items = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, items))

  

[Python] map Method

原文:http://www.cnblogs.com/KennyRom/p/6607289.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!