numpy.trim_zeros function is used to trim the leading and/or trailing zeros from a 1-D array or sequence.
Syntax: numpy.trim_zeros(arr, trim)
Parameters:
arr : 1-D array or sequence
trim : trim is an optional parameter with default value to be ‘fb'(front and back) we can either select ‘f'(front) and ‘b’ for back.
Returns: trimmed : 1-D array or sequence (without leading and/or trailing zeros as per user’s choice)
Code 1:
import numpy as geek
gfg = geek.array((0, 0, 0, 0, 1, 5, 7, 0, 6, 2, 9, 0, 10, 0, 0))
res = geek.trim_zeros(gfg)
print(res)
|
Output :array([1, 5, 7, 0, 6, 2, 9, 0, 10])
Code 2:
import numpy as geek
gfg = geek.array((0, 0, 0, 0, 1, 5, 7, 0, 6, 2, 9, 0, 10, 0, 0))
res = geek.trim_zeros(gfg, 'f')
print(res)
|
Output :array([1, 5, 7, 0, 6, 2, 9, 0, 10, 0, 0])
Code 3:
import numpy as geek
gfg = geek.array((0, 0, 0, 0, 1, 5, 7, 0, 6, 2, 9, 0, 10, 0, 0))
res = geek.trim_zeros(gfg, 'b')
print(res)
|
Output :array([0, 0, 0, 0, 1, 5, 7, 0, 6, 2, 9, 0, 10])
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.Dive into the future of technology - explore the
Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
27 Sep, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...