In Python 2 I could do the following:
import numpy as np
f = lambda x: x**2
seq = map(f, xrange(5))
seq = np.array(seq)
print seq
# prints: [ 0 1 4 9 16]
In Python 3 it does not work anymore:
import numpy as np
f = lambda x: x**2
seq = map(f, range(5))
seq = np.array(seq)
print(seq)
# prints: <map object at 0x10341e310>
How do I get the old behaviour (converting the map
results to numpy
array)?
Edit: As @jonrsharpe pointed out in his answer this could be fixed if I converted seq
to a list first:
seq = np.array(list(seq))
but I would prefer to avoid the extra call to list
.
Although you refer to it as
seq
, themap
object in Python 3 is not a sequence (it's an iterator, see what's new in Python 3).numpy.array
needs a sequence so thelen
can be determined and the appropriate amount of memory reserved; it won't consume an iterator. For example, therange
object, which does support most sequence operations, can be passed directly;To restore the previous behaviour, as you're aware, you can explicitly convert the
map
object back to a sequence (e.g. list or tuple):However, as the documentation puts it:
So another option would be:
or just:
Alternatively, actually use
numpy
from the start:One more alternative, other than the valid solutions @jonrsharpe already pointed out is to use
np.fromiter
: