map filter reduce

#code:utff-8
import sys
from functools import reduce
# map
#允许接受两个参数,第一个为函数,或者为函数表达式,第二个参数为可迭代对象
# 返回list
def test(x):
    return x * 2
t = [1, 2, 3, 4]
for tmp in map(test, t):
    print(tmp)
# filter
# 允许接受两个参数第一个为函数,或者为函数表达式,但是返回值为布尔型,第二参数可迭代对象
#返回 满足函数判断条件的list
def test1(x):
    return x < 2
for tmp1 in filter(test1, t):
    print(tmp1)
#reduce 与map 类似,reduce允许函数传入两个参数
def test2(x, y):
    return  x + y
print(reduce(test2, t))

  

posted @ 2017-12-29 11:17  我是外婆  阅读(90)  评论(0编辑  收藏  举报