LoginSignup
0
1

More than 5 years have passed since last update.

Python 3移行 > TypeError: must be real number, not map の対応 > map()をlist(map())にする

Last updated at Posted at 2017-03-17
動作環境
Xeon E5-2620 v4 (8コア) x 2
32GB RAM
CentOS 6.8 (64bit)
openmpi-1.8.x86_64 とその-devel
mpich.x86_64 3.1-5.el6とその-devel
gcc version 4.4.7 (とgfortran)
NCAR Command Language Version 6.3.0
WRF v3.7.1を使用。
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 
Python 3.6.0 on virtualenv

以下のコードは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)

Python 3で実行すると、以下のエラーが出る。

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

Python 2ではmap()で返るものはlist objectだった。
Python 3ではmap()で返るものはmap objectになっているようだ。

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

map objectをlist()でくくることで、エラーは出なくなった。

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)
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1