Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get start and stop from a python slice object

Tags:

python

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.

like image 474
m_amber Avatar asked Mar 18 '15 19:03

m_amber


People also ask

What is the syntax for slicing step start stop?

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.

How do you slice an object in Python?

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.

How do you override a slice in Python?

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.

What is :: In slicing?

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.


1 Answers

The members of a slice are start, step and stop:

Slice objects have read-only data attributes start, stop and step 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)