1. 程式人生 > >TypeError: unsupported operand type(s) for /: 'map' and 'int'

TypeError: unsupported operand type(s) for /: 'map' and 'int'

出現下面這樣的錯誤:

ret, rcount, out=ret, casting='unsafe', subok=False)
TypeError: unsupported operand type(s) for /: 'map' and 'int'

出錯位置:

dataArr = [map(float,line)for line in stringArr]
return mat(dataArr)

出錯原因:

map()函式的結果是一個IteratorIterator是惰性序列,因此通過list()函式讓它把整個序列都計算出來並返回一個list。

>>> list(map(str, [1, 2, 3, 4, 5, 6, 7, 8, 9]))
['1', '2', '3', '4', '5', '6', '7', '8', '9']

修改方法:

只需要在程式中對應的map處加一個list即可

dataArr = [list(map(float,line)) for line in stringArr]
return mat(dataArr)