你也可以使用
.shape
属性,它会返回一个元组,其中包含
每个维度的长度。因此,要使用
.shape
获取维度,你也可以在结果元组上调用
len()
:
import numpy as np
a = np.array([1,2,3])
b = np.array([[1,2,3]])
c = np.array([[1,2,3],[2,4,6],[3,6,9]])
print("a={}".format(a))
print("a.shape: {}; len(a.shape): {}".format(a.shape, len(a.shape)))
print("b={}".format(b))
print("b.shape: {}; len(b.shape): {}".format(b.shape, len(b.shape)))
print(c)
print("c.shape: {}; len(c.shape): {}".format(c.shape, len(c.shape)))
输出:
a=[1 2 3]
a.shape: (3,); len(a.shape): 1
b=[[1 2 3]]
b.shape: (1, 3); len(b.shape): 2
[[1 2 3]
[2 4 6]
[3 6 9]]
c.shape: (3, 3); len(c.shape): 2