如何检查 NumPy 数组是否为 1D 数组

使用 .ndim == 1,它包含数组中的维数。

how-to-check-if-a-numpy-array-is-a-1d-array.py
if a.ndim == 1:
    print('a 是 1D 数组')
else:
    print('a 不是 1D 数组')

你也可以使用 assert 来检查:

assert_1d_array.py
assert a.ndim == 1

如果 a 不是一维数组,将引发 AssertionError


Check out similar posts by category: Python