Python怎么获取numpy数组的维度?

1 个回答|42 次阅读
Coco老师 - 科普专家
擅长:编程教育
import numpy as np
a = np.array(42)
b = np.array([1, 2, 3, 4, 5])
c = np.array([[1, 2, 3], [4, 5, 6]])
d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(a.ndim)
print(b.ndim)
print(c.ndim)
print(d.ndim)

执行结果:

0
1
2
3