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

[Python] map Method

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

标签:default   div   strong   blueprint   int   append   -o   and   blog   

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

标签:default   div   strong   blueprint   int   append   -o   and   blog   

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

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