如何删除numpy数组的前导/尾随零?
import numpy as np a = np.array([0,0,0,3,2,-1,0,0,7,9,13,0,0,0,0,0,0,0]) #Desired output [3,2,-1,0,0,7,9,13]
这不起作用:
a[a != 0]
因为它会删除所有零,包括里面的零.
用途numpy.trim_zeros
:
>>> import numpy as np >>> a = np.array([0,0,0,3,2,-1,0,0,7,9,13,0,0,0,0,0,0,0]) >>> np.trim_zeros(a) array([ 3, 2, -1, 0, 0, 7, 9, 13])