ENH: helper for multidimensional slicing
See original GitHub issueIn thinking about #10771, I realized that a simple helper function like the following might be handy:
def ndslice(sl):
""" convert a slice of iterables into a tuple of slices (nd slice)"""
start = sl.start
stop = sl.stop
step = sl.step
if start is None and stop is None and step is None:
return ()
if start is None:
start = itertools.repeat(None)
if stop is None:
stop = itertools.repeat(None)
if step is None:
step = itertools.repeat(None)
return tuple(
slice(art, op, ep)
for art, op, ep in zip(start, stop, step)
)
Which could be used as:
a = np.zeros((10, 10, 10))
a[ndslice(np.s_[[1,2,3]:[4,5,6]])] # same as a[1:4, 2:5, 3:6]
Or possibly using a __getitem__
method like np.s_[]
, sugared to:
class ndslice_type(object):
__getitem__ = lambda self, item: ndslice(item)
ndslice_ = ndslice_type()
a[ndslice_[[1,2,3] : [4,5,6]]] # same as a[1:4, 2:5, 3:6]
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (8 by maintainers)
Top Results From Across the Web
Numpy multidimensional array slicing - python - Stack Overflow
Now, I can get an array containing the (0,1) element of each 3x3 subarray with x[:, 0, 1] , which returns array([ 1,...
Read more >Chapter 4. Arrays, slices, and maps - Go in Action
Array internals and fundamentals; Managing collections of data with slices; Working with key/value pairs using maps;
Read more >Multidimensional arrays: initial values and slicing (#15) - GitLab
The purpose of this issue is to gather a few features related to multidimensional arrays, which I would expect to be supported but...
Read more >Indexing and Slicing of 1D, 2D and 3D Arrays Using Numpy
Generate a two-dimensional array using arange and reshape function. I made a 6×7 matrix for this video. Because it is big enough to...
Read more >Is There any way to fix Column Widths in a Matrix - Microsoft Power ...
Specifically, I have a matrix that has two Values per Column Item: ... please let me know) or is this an enhancement that...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You can’t combine
...
with a tuple. So that would need to be:np.ndslice_[...,::step_tup]
? Not sure what the semantics for...
are in ndslice though?