MCPcopy
hub / github.com/numpy/numpy / slice

Function slice

numpy/_core/strings.py:1730–1813  ·  view source on GitHub ↗

Slice the strings in `a` by slices specified by `start`, `stop`, `step`. Like in the regular Python `slice` object, if only `start` is specified then it is interpreted as the `stop`. Parameters ---------- a : array-like, with ``StringDType``, ``bytes_``, or ``str_`` dtype

(a, start=None, stop=np._NoValue, step=None, /)

Source from the content-addressed store, hash-verified

1728
1729@set_module("numpy.strings")
1730def slice(a, start=None, stop=np._NoValue, step=None, /):
1731 """
1732 Slice the strings in `a` by slices specified by `start`, `stop`, `step`.
1733 Like in the regular Python `slice` object, if only `start` is
1734 specified then it is interpreted as the `stop`.
1735
1736 Parameters
1737 ----------
1738 a : array-like, with ``StringDType``, ``bytes_``, or ``str_`` dtype
1739 Input array
1740
1741 start : None, an integer or an array of integers
1742 The start of the slice, broadcasted to `a`'s shape
1743
1744 stop : None, an integer or an array of integers
1745 The end of the slice, broadcasted to `a`'s shape
1746
1747 step : None, an integer or an array of integers
1748 The step for the slice, broadcasted to `a`'s shape
1749
1750 Returns
1751 -------
1752 out : ndarray
1753 Output array of ``StringDType``, ``bytes_`` or ``str_`` dtype,
1754 depending on input type
1755
1756 Examples
1757 --------
1758 >>> import numpy as np
1759 >>> a = np.array(['hello', 'world'])
1760 >>> np.strings.slice(a, 2)
1761 array(['he', 'wo'], dtype='<U5')
1762
1763 >>> np.strings.slice(a, 2, None)
1764 array(['llo', 'rld'], dtype='<U5')
1765
1766 >>> np.strings.slice(a, 1, 5, 2)
1767 array(['el', 'ol'], dtype='<U5')
1768
1769 One can specify different start/stop/step for different array entries:
1770
1771 >>> np.strings.slice(a, np.array([1, 2]), np.array([4, 5]))
1772 array(['ell', 'rld'], dtype='<U5')
1773
1774 Negative slices have the same meaning as in regular Python:
1775
1776 >>> b = np.array(['hello world', 'γεια σου κόσμε', '你好世界', '👋 🌍'],
1777 ... dtype=np.dtypes.StringDType())
1778 >>> np.strings.slice(b, -2)
1779 array(['hello wor', 'γεια σου κόσ', '你好', '👋'], dtype=StringDType())
1780
1781 >>> np.strings.slice(b, -2, None)
1782 array(['ld', 'με', '世界', ' 🌍'], dtype=StringDType())
1783
1784 >>> np.strings.slice(b, [3, -10, 2, -3], [-1, -2, -1, 3])
1785 array(['lo worl', ' σου κόσ', '世', '👋 🌍'], dtype=StringDType())
1786
1787 >>> np.strings.slice(b, None, None, -1)

Callers 15

stackFunction · 0.85
_concatenate_shapesFunction · 0.85
_cumulative_funcFunction · 0.85
rollFunction · 0.85
_indices_for_nelemsFunction · 0.85
random_sliceFunction · 0.85
random_slice_fixed_sizeFunction · 0.85
check_unary_fuzzMethod · 0.85

Calls 1

anyMethod · 0.45

Tested by 15

_indices_for_nelemsFunction · 0.68
random_sliceFunction · 0.68
random_slice_fixed_sizeFunction · 0.68
check_unary_fuzzMethod · 0.68
test_iter_best_orderFunction · 0.68
test_iter_c_orderFunction · 0.68
test_iter_f_orderFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…