TypeError with map with no len()

Terry Reedy tjreedy at udel.edu
Mon Sep 25 13:01:07 EDT 2017


On 9/25/2017 12:44 PM, john polo wrote:
> Python List,
> 
> I am trying to make practice data for plotting purposes. I am using 
> Python 3.6. The instructions I have are
> 
> import matplotlib.pyplot as plt
> import math
> import numpy as np
> t = np.arange(0, 2.5, 0.1)
> y1 = map(math.sin, math.pi*t)

Change to
y1 = list(map(math.sin, math.pi*t))
or
y1 = tuple(map(math.sin, math.pi*t))
(Does not make much difference.)

> plt.plot(t,y1)

or
plt.plot(t, list(y1))

> However, at this point, I get a TypeError that says
> 
> object of type 'map' has no len()

In 3.x, map returns an iterator.  plot needs a sequence.

> In [6]: t
> Out[6]:
> array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7, 0.8,  0.9,  1. ,
>          1.1,  1.2,  1.3,  1.4,  1.5,  1.6,  1.7,  1.8, 1.9,  2. ,  2.1,
>          2.2,  2.3,  2.4])
> In [7]: y1
> Out[7]: <map at 0x6927128>
> In [8]: math.pi*t
> Out[8]:
> array([ 0.        ,  0.31415927,  0.62831853,  0.9424778 , 1.25663706,
>          1.57079633,  1.88495559,  2.19911486,  2.51327412, 2.82743339,
>          3.14159265,  3.45575192,  3.76991118,  4.08407045, 4.39822972,
>          4.71238898,  5.02654825,  5.34070751,  5.65486678, 5.96902604,
>          6.28318531,  6.59734457,  6.91150384,  7.2256631 , 7.53982237])
> 
> At the start of creating y1, it appears there is an array to iterate 
> through for the math.sin function used in map(), but y1 doesn't appear 
> to be saving any values. I expected y1 to hold a math.sin() output for 
> each item in math.pi*t, but it appears to be empty. Am I 
> misunderstanding map()?

See comment 3 above.

>Is there something else I should be doing 
> instead to populate y1?

See comment 1 (or 2) above.

-- 
Terry Jan Reedy





More information about the Python-list mailing list