Python 3 migration> TypeError: must be real number, not map support> change map () to list (map ())

Operating environment


Xeon E5-2620 v4 (8 cores) x 2
32GB RAM
CentOS 6.8 (64bit)
openmpi-1.8.x86_64 and its-devel
mpich.x86_64 3.1-5.el6 and its-devel
gcc version 4.4.7 (And gfortran)
NCAR Command Language Version 6.3.0
WRF v3.7.Use 1.
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 
Python 3.6.0 on virtualenv

The code below was fine in Python 2.

test_numpy_170317.py


#!/usr/bin/env python

import numpy as np

vals = map(float, [3., 1., 4.])
total_val = np.sum(vals)
print('total: %.2f' % total_val)

When I run it in Python 3, I get the following error:

Traceback (most recent call last):
  File "test_numpy_170317.py", line 7, in <module>
    print('total: %.2f' % total_val)
TypeError: must be real number, not map

In Python 2, what map () returns is a list object. In Python 3, what is returned by map () seems to be a map object.

Reference http://stackoverflow.com/questions/1303347/getting-a-map-to-return-a-list-in-python-3-x

By enclosing the map object in list (), the error disappeared.

test_numpy_170317.py


#!/usr/bin/env python

import numpy as np

vals = list(map(float, [3., 1., 4.]))
total_val = np.sum(vals)
print('total: %.2f' % total_val)

Recommended Posts

Python 3 migration> TypeError: must be real number, not map support> change map () to list (map ())
Did not change from Python 2 to 3
TypeError: mul (): argument'other' (position 1) must be Tensor, not list Solution for error
What to do when "TypeError: must be string, not int…" appears when using strptime
It was a painful memory when I was told TypeError: must be type, not class obj when trying to inherit with Python.
[python] Change the image file name to a serial number
[Python] It might be useful to list the data frames