map = [ [1, 1, 1, 1, 1, 1, 1, 0, 1, 1],
[1, 0, 1, 0, 0, 0, 1, 1, 1, 1],
[1, 0, 1, 1, 1, 1, 0, 1, 1, 0 ],
[0, 1, 1, 1, 0, 0, 1, 1, 1, 1 ],
[1, 1, 1, 0, 0, 1, 1, 1, 0, 1 ],
[1, 1, 0, 1, 1, 1, 1, 1, 1, 1 ],
[1, 1, 1, 1, 0, 1, 1, 1, 1, 1 ],
[0, 0, 0, 1, 1, 1, 1, 0, 0, 0 ],
[1, 0, 0, 0, 1, 0, 1, 1, 1, 1 ],
[0, 1, 1, 0, 0, 1, 1, 1, 1, 1 ]]
map_input = np.ndarray(map)
# 会报错:TypeError: 'list' object cannot be interpreted as an integer
本意就是想把二维list转为numpy的ndarray类型,因为list类型实际上没有shape属性,作为二维矩阵不好用。
原因可能是ndarray是一个class,初始化函数中,无法把一个二维list初始化,只能对一维list进行初始化。
什么是ndarray? 这是numpy下面的一个class,是numpy的核心特征,核心数据结构,是python中一个快速的,灵活的大型数据集容器,可以轻松地对其中的数据进行科学运算。
map = [ [1, 1, 1, 1, 1, 1, 1, 0, 1, 1],
[1, 0, 1, 0, 0, 0, 1, 1, 1, 1],
[1, 0, 1, 1, 1, 1, 0, 1, 1, 0 ],
[0, 1, 1, 1, 0, 0, 1, 1, 1, 1 ],
[1, 1, 1, 0, 0, 1, 1, 1, 0, 1 ],
[1, 1, 0, 1, 1, 1, 1, 1, 1, 1 ],
[1, 1, 1, 1, 0, 1, 1, 1, 1, 1 ],
[0, 0, 0, 1, 1, 1, 1, 0, 0, 0 ],
[1, 0, 0, 0, 1, 0, 1, 1, 1, 1 ],
[0, 1, 1, 0, 0, 1, 1, 1, 1, 1 ]]
map_input = np.array(map)
print(type(map_input))
# 输出
<class 'numpy.ndarray'>