Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Matrix manipulation in Python
Next article icon

numpy.trim_zeros()removes the leading and trailing zeros from a 1-D array. It is often used to clean up data by trimming unnecessary zeros from the beginning or end of the array.Example:

Python
importnumpyasnpa=np.array([0,0,3,4,0,5,0,0])res=np.trim_zeros(a)print(res)

Output
[3 4 0 5]

Explanation: np.trim_zeros(a)removes both leading and trailing zeros by default (trim='fb'). Zeros in the middle remain unchanged.

Syntax

numpy.trim_zeros(filt, trim='fb')

Parameter:

  • filt (array_like): A 1-D array of numeric values from which zeros will be trimmed.
  • trim (str, optional): Indicates which zeros to remove, 'f' for leading (front), 'b' for trailing (back) and 'fb' (default) for both.

Returns:A new 1-D NumPy array with the specified zeros removed.

Examples

Example 1:In this example, we use the parameter trim='f' to remove the leading zeros from the beginning of the array, while keeping the zeros at the end unchanged.

Python
importnumpyasnpa=np.array([0,0,1,2,3,0])res=np.trim_zeros(a,trim='f')print(res)

Output
[1 2 3 0]

Explanation: np.trim_zeros(a, trim='f')removes only the leading zeros from the beginning of the array. Trailing zeros are preserved, resulting in [1, 2, 3, 0].

Example 2:In this example, we use the parameter trim='b' to remove the trailing zeros from the end of the array, while keeping the leading zeros at the beginning unchanged.

Python
importnumpyasnpa=np.array([0,1,2,0,0])res=np.trim_zeros(a,trim='b')print(res)

Output
[0 1 2]

Explanation: np.trim_zeros(a, trim='b') removes only the trailing zeros from the end of the array. Leading zeros are kept intact, resulting in [0, 1, 2].

Example 3: In this example, all elements are zeros. Sincenp.trim_zeros()removes zeros from both ends by default, the result is an empty array.

Python
importnumpyasnpa=np.array([0,0,0,0])res=np.trim_zeros(a)print(res)

Output
[]

Explanation: np.trim_zeros(a) removes zeros from both ends by default (trim='fb'). Since the array contains only zeros, the result is an empty array.


Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp