I'm using the scipy ndimage modules and I'm using the find_objects function, which returns a slice object. Now i want to read this slice object and get the start and end indices from them. However, i can't find the way to do that.
fragments = ndimage.find_objects(labels)
print fragments[0][0]
> slice(0L, 832L, None)
How do i store the start and end indices as variables in my python code.
Any help will be appreciated. Thanks in advance.
Slicing is a way of getting subsets of data structures. The basic notation is: [start:stop:step] The default for start is none or 0 . The default stop is the end of your data structure.
Python slice() FunctionThe slice() function returns a slice object. A slice object is used to specify how to slice a sequence. You can specify where to start the slicing, and where to end. You can also specify the step, which allows you to e.g. slice only every other item.
You need to provide custom __getitem__() , __setitem__ and __delitem__ hooks. These are passed a slice object when slicing the list; these have start , stop and step attributes. However, these values could be None , to indicate defaults.
Consider a python list, In-order to access a range of elements in a list, you need to slice a list. One way to do this is to use the simple slicing operator i.e. colon(:) With this operator, one can specify where to start the slicing, where to end, and specify the step.
The members of a slice
are start
, step
and stop
:
Slice objects have read-only data attributes
start
,stop
andstep
which merely return the argument values (or their default).
Thus as simple as:
>>> sl = slice(0L, 832L)
>>> sl
slice(0L, 832L, None)
>>> start = sl.start
>>> stop = sl.stop
>>> start, stop
(0L, 832L)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With