【发布时间】:2019-06-04 02:20:20
【问题描述】:
为什么我在 python 中使用函数 map() 时得到一个错误的列表?这是我的代码,
当我使用 print() 函数打印 list(r) 时
print(list(r))
我的结果是一个空列表。
但是当我写代码的时候:
rList= list(r)
然后
print(rList)
我的结果是对的。
这是我的代码:
def f(x):
return x * x
r = map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9])
rList= list(r)
print( list(r) )
print( rList)
这是我的结果:
[]
[1,4,9,16,25,36,49,64,81]
这是预期的结果
[1,4,9,16,25,36,49,64,81]
[1,4,9,16,25,36,49,64,81]
【问题讨论】:
-
感觉这个问题我好像见过……
-
@RoadRunner 不错,你。
-
@coldspeed 不用担心,我会确保下次投票结束。
标签: python list dictionary printing mapreduce