'list' object has no attribute 'reshape'怎么办
时间: 2023-12-18 17:30:25 浏览: 355
当出现'list' object has no attribute 'reshape'错误时,说明你正在尝试对一个列表对象进行reshape操作,而reshape操作只能在numpy数组上进行。要解决这个问题,你需要将列表转换为numpy数组,然后再进行reshape操作。
以下是一个示例代码,演示了如何解决'list' object has no attribute 'reshape'错误:
```python
import numpy as np
def image2vector(image):
"""
Argument:
image -- a numpy array of shape (length, height, depth)
Returns:
v -- a vector of shape (length*height*depth, 1)
"""
v = np.array(image).reshape(image.shape[0]*image.shape[1]*image.shape[2], 1)
return v
a = [[[1,2,3], [3,4,5]], [[5,6,7], [8,9,10]]]
s = image2vector(a)
print(s)
```
在这个示例中,我们首先将列表a转换为numpy数组,然后再对其进行reshape操作。这样就可以避免出现'list' object has no attribute 'reshape'错误。
阅读全文