python list 转化为矩阵报错:AttributeError: 'list' object has no attribute 'reshape'

报错类型

>>> b
['1', '0.1', '1', '0.1']
>>> b.reshape(2,2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'reshape'

解决方案

list不能使用reshape,需要将其转化为array,然后就可以使用reshape

>>> import numpy as np
>>> np.array(b).reshape(2,2)
array([['1', '0.1'],
       ['1', '0.1']], dtype='<U3')

搞定,过来点一下赞吧。

发布了245 篇原创文章 · 获赞 89 · 访问量 22万+

猜你喜欢

转载自blog.csdn.net/yijiaobani/article/details/102957153