从python切片对象开始和停止

2024-05-17 18:06:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是scipy ndimage模块和find_objects函数,它返回一个slice对象。现在我想读取这个slice对象并从中获取开始和结束索引。但是,我找不到办法。在

fragments = ndimage.find_objects(labels)
print fragments[0][0]
> slice(0L, 832L, None)

如何在python代码中将开始和结束索引存储为变量。在

任何帮助都将不胜感激。提前谢谢。在


Tags: 模块对象函数代码nonelabelsobjectsslice
1条回答
网友
1楼 · 发布于 2024-05-17 18:06:57

^{}的成员是startstep和{}:

Slice objects have read-only data attributes start, stop and step which merely return the argument values (or their default).


因此简单到:

>>> sl = slice(0L, 832L)
>>> sl
slice(0L, 832L, None)
>>> start = sl.start
>>> stop = sl.stop
>>> start, stop
(0L, 832L)

相关问题 更多 >