python map object at

2019-01-28 19:47发布

问题:

im new to Python (version 3.41) and I have a problem with printing the result of using map and filter function. How can I find these results?

>>> def double(n):
    return n*2

>>> li = [1,2,3]
>>> map(double, li)
<map object at 0x000000000333DCF8>
>>> print(map(double,li))
<map object at 0x000000000333DC50>

回答1:

Try either :

print(list(map(double,li))

Or :

for item in map(double,li):
    print(item)

In Python 3.4 map will return an iterator instead of a list (as it did in 2.7+). This is great as far as memory footprint is concerned but it doesn't map well to printing (pun intended)