Reputation: 1948
If I have a slice object
s = slice(a,b,c)
and an array length n
, is there a nice readymade iterator for the elements so that I can do something like:
for index in FUNCTION_I_WANT(s, n):
do_whatever(index)
and have it behave like slicing of lists, beyond the really horrible:
def HACKY_VERSION_OF_FUNCTION_I_WANT(s,n):
yield range(n).__getitem__(s)
Upvotes: 7
Views: 1721
Reputation: 798754
def FUNCTION_I_WANT(s, n):
return range(*s.indices(n))
Upvotes: 15