map()3

# -*- coding: utf-8 -*-
#python 27
#xiaodeng
#map()3
'''
map(...)
    map(function, sequence[, sequence, ...]) -> list
'''


#由于map期待传入一个函数,他恰好是lambda通常出现的地方之一
counters=[1,2,3,4]
print list(map((lambda x:x+3),counters))#[4, 5, 6, 7]


#以上功能等同于:
def mymap(func,seq):
    res=[]
    for x in seq:
        res.append(func(x))
    return res

def inc(x):
    return x+3
print mymap(inc,[1,2,3,4])#[4, 5, 6, 7]

 

posted @ 2015-10-25 20:57  Xiao|Deng  阅读(106)  评论(0编辑  收藏  举报