numpy.trim_zeros () Function with example in python
numpy.trim_zeros(): This function trim the leading and/or trailing zeros from a 1-D array or sequence.
syntax: numpy.trim_zeros(filt, trim=’fb’)[source]
Parameters:
filt : 1-D array or sequence
Input array.
trim : str, optional
A string with ‘f’ representing trim from front and ‘b’ to trim from back. Default is ‘fb’, trim zeros from both front and back of the array.
Returns:
trimmed : 1-D array or sequence
The result of trimming the input. The input data type is preserved.
import numpy as np
a=np.array([1,2,3,4,5,6,0,0,0])
b=np.trim_zeros(a)
print(b)
Output:
[1,2,3,4,5,6]