对一个列表[1,3,5,7,8,9,4] 用map返回乘方列表;reduce计算乘积、filter挑出奇数
a=[1, 3, 5, 7, 8, 9, 4]
def func_1(x):
""" 返回乘方列表 """
return x*x
def func_2(x, y):
""" 返回乘积 """
return x*y
def func_3(x):
""" 挑出奇数 """
if (x%2 != 0):
return x
if __name__ == '__main__':
print "main()"
# 用map返回乘方列表
b = map(func_1, a)
print "类型:%s 输出: %s"%(type(b), b)
# reduce计算乘积
b = reduce(func_2, a);
print "类型:%s 输出: %s"%(type(b), b)
# filter挑出奇数
b = filter(func_3, a)
print "类型:%s 输出: %s"%(type(b), b)
# sorted排序反序输出
b = sorted(a,reverse=True)
print "类型:%s 输出: %s"%(type(b), b)
输出结果:
main()
类型:<type 'list'> 输出: [1, 9, 25, 49, 64, 81, 16]
类型:<type 'int'> 输出: 30240
类型:<type 'list'> 输出: [1, 3, 5, 7, 9]
类型:<type 'list'> 输出: [9, 8, 7, 5, 4, 3, 1]