Python中使用map()和filter()的异常输出

时间:2014-12-02 13:01:21

标签: python map filter

我试图学习如何在Python中使用map()和filter()函数,但是当我尝试在visual studio中使用它们时,我会为每个函数获得一个不寻常的输出。我知道代码可能是错误的,但是我无法看到它的输出是什么让你难以理清!

提前致谢

过滤器()

import functools

f = ["List", "of", "super", "crazily", "long", "words"]

new = lambda a, b: a if (len(a) > b) else b

print (filter(new, f))

服务:过滤器对象位于0x029AD5F0

地图()

import functools

f = ["List", "of", "super", "crazily", "long", "words"]

map_loop = map((lambda x: len(x)), f)

print (type(map_loop), map_loop) 

服务:课程'地图',地图对象位于0x0243D5D0

1 个答案:

答案 0 :(得分:-1)

你需要像这样使用list

print (type(map_loop), list(map_loop)) 

演示:

>>> f = ["List", "of", "super", "crazily", "long", "words"]
>>> print(list(map(len,f)))     # no need of lembda,  `len` is enough
[4, 2, 5, 7, 4, 5]