Trim Zeros of a 1d array: Difference between revisions

From Master of Neuroscience Wiki
Created page with "== Top == Questions to [mailto:davrot@uni-bremen.de David Rotermund] == [https://numpy.org/doc/stable/reference/generated/numpy.trim_zeros.html numpy.trim_zeros] == <syntaxhighlight lang="python">numpy.trim_zeros(filt, trim='fb')</syntaxhighlight><blockquote>Trim the leading and/or trailing zeros from a 1-D array or sequence.</blockquote><syntaxhighlight lang="python">import numpy as np a = np.arange(0, 10) b = np.zeros((5), dtype=np.int64) c = np.r_[b, a, b] print(c)..."
(No difference)

Revision as of 13:46, 17 October 2025

Top

Questions to David Rotermund

numpy.trim_zeros

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

Trim the leading and/or trailing zeros from a 1-D array or sequence.

import numpy as np

a = np.arange(0, 10)
b = np.zeros((5), dtype=np.int64)
c = np.r_[b, a, b]
print(c)  # -> [0 0 0 0 0 0 1 2 3 4 5 6 7 8 9 0 0 0 0 0]

print(np.trim_zeros(c, "b")) # -> [0 0 0 0 0 0 1 2 3 4 5 6 7 8 9]
print(np.trim_zeros(c, "f")) # -> [1 2 3 4 5 6 7 8 9 0 0 0 0 0]
print(np.trim_zeros(c, "fb")) # -> [1 2 3 4 5 6 7 8 9]