Testing if an index is in a slice
Aaron Brady
castironpi at gmail.com
Thu Jan 1 17:27:45 EST 2009
On Jan 1, 12:12 pm, mma... at gmx.net wrote:
> Hi
>
> I would like to check if an index is in a slice or not without
> iterating over the slice.
>
> Something like:
>
> >>> isinslice(36, slice(None, 34, -1))
>
> True
>
> I would like to use the batteries if possible.
> However, I looked in the docs, pypi and in Usenet without luck.
>
> Does anyone have a solution?
>
> Martin
Untested:
if slice.end< 0:
slice.end= len( obj )- slice.end+ 1
if slice.step< 0 and slice.start<= target< slice.end:
return True
if slice.step> 0 and slice.start>= target> slice.end:
return True
return False
More information about the Python-list
mailing list