Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

resolving map function issue in python 3 vs python 2

I'm interested in functional programming with python and am working through Mary Rose Cook's blog post A practical introduction to functional programming.

Apparently, it was written in python 2 as this:

name_lengths = map(len, ["Mary", "Isla", "Sam"])

print name_lengths
# => [4, 4, 3]

in Python 3 yields this:

<map object at 0x100b87a20>

I have two questions:

  1. Why is this is so?
  2. Other than converting the map object to a list and then use numpy, are there any other solutions?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As documented, in the migration guide,

In Python 2 map() returns a list while in Python 3 it returns an iterator.

Python 2:

Apply function to every item of iterable and return a list of the results.

Python 3:

Return an iterator that applies function to every item of iterable, yielding the results.

Python 2 always does the equivalent of list(imap(...)), Python 3 allows for lazy evaluation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...