python-报错:ValueError: setting an array element with a sequence.

报错场景:

一般在使用numpy.reshape(a,new_shape)函数时出现;

报错信息:

ValueError: setting an array element with a sequence.

原因:

被转换列表a中存在异常序列

import numpy as np

a1 = [1,2,3,4,5,6,7,8,9]
b1 = np.reshape(a1,(3,3))  # Correct

a2 = [[1,2,3],[4,5,6],[7,8,9]]
b2 = np.reshape(a2,(3,3))  # Correct

a3 = [1,2,3,4,5,6,7,8,[9]]
b3 = np.reshape(a3,(3,3))  # ValueError:setting an array element with a sequence.

猜你喜欢

转载自blog.csdn.net/qq_17753903/article/details/88094934