import numpy as np
a = np.array([[0,2, 0],
[0,5, 0],
[0,8, 0]])
del_index = []
for col in range(3):
if a[0][col] == 0:
del_index.append(col)
print(del_index)
a = np.delete(a, del_index, axis = 1) # axis=1 删除列,axis=0 删除行
print(a)
输出:
[0, 2] # 要删除的列
# 删除后的结果
[[2]
[5]
[8]]