出错原因:
在python3里面,map()的返回值是iterators,而不是list, 所以想要使用,需将iterator 转换成list
# 声明函数square(平方)
def square(x):
return x**3
# 执行报错
# 查看类型
# 改为list类型
print (map(square, [1,2,3,4,5]))
print (type(map(square, [1,2,3,4,5])))
print (list(map(square, [1,2,3,4,5])))
def sum(x, y):
return x + y
print (list(map(sum, [1,2,3,4,5],[2,2,2,2,2])))
运行结果:
<map object at 0x00B9C028>
<class 'map'>
[1, 8, 27, 64, 125]
[3, 4, 5, 6, 7]