MCPcopy Create free account
hub / github.com/numpy/numpy / trim_zeros

Function trim_zeros

numpy/lib/function_base.py:1805–1854  ·  view source on GitHub ↗

Trim the leading and/or trailing zeros from a 1-D array or sequence. 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 zero

(filt, trim='fb')

Source from the content-addressed store, hash-verified

1803
1804@array_function_dispatch(_trim_zeros)
1805def trim_zeros(filt, trim='fb'):
1806 """
1807 Trim the leading and/or trailing zeros from a 1-D array or sequence.
1808
1809 Parameters
1810 ----------
1811 filt : 1-D array or sequence
1812 Input array.
1813 trim : str, optional
1814 A string with 'f' representing trim from front and 'b' to trim from
1815 back. Default is 'fb', trim zeros from both front and back of the
1816 array.
1817
1818 Returns
1819 -------
1820 trimmed : 1-D array or sequence
1821 The result of trimming the input. The input data type is preserved.
1822
1823 Examples
1824 --------
1825 >>> a = np.array((0, 0, 0, 1, 2, 3, 0, 2, 1, 0))
1826 >>> np.trim_zeros(a)
1827 array([1, 2, 3, 0, 2, 1])
1828
1829 >>> np.trim_zeros(a, 'b')
1830 array([0, 0, 0, ..., 0, 2, 1])
1831
1832 The input data type is preserved, list/tuple in means list/tuple out.
1833
1834 >>> np.trim_zeros([0, 1, 2, 0])
1835 [1, 2]
1836
1837 """
1838
1839 first = 0
1840 trim = trim.upper()
1841 if 'F' in trim:
1842 for i in filt:
1843 if i != 0.:
1844 break
1845 else:
1846 first = first + 1
1847 last = len(filt)
1848 if 'B' in trim:
1849 for i in filt[::-1]:
1850 if i != 0.:
1851 break
1852 else:
1853 last = last - 1
1854 return filt[first:last]
1855
1856
1857def _extract_dispatcher(condition, arr):

Callers 9

__init__Method · 0.90
test_basicMethod · 0.90
test_leading_skipMethod · 0.90
test_trailing_skipMethod · 0.90
test_all_zeroMethod · 0.90
test_size_zeroMethod · 0.90
test_overflowMethod · 0.90
test_no_trimMethod · 0.90
test_list_to_listMethod · 0.90

Calls 1

upperMethod · 0.80

Tested by 8

test_basicMethod · 0.72
test_leading_skipMethod · 0.72
test_trailing_skipMethod · 0.72
test_all_zeroMethod · 0.72
test_size_zeroMethod · 0.72
test_overflowMethod · 0.72
test_no_trimMethod · 0.72
test_list_to_listMethod · 0.72