slicing return iter?
python at rcn.com
Mon Oct 19 03:07:40 CEST 2009
> > If it really is a sequence (with len and getitem), you can write your
> > own indexing iterator:
> > def myslice(seq, start, stop, step):
> > 'Allow forward or backwards iteration over a subslice'
> > for i in range(start, stop, step):
> > yield seq[i]
> Thank you. but it can't support negative index :-(
The negative index is handled by the line, "yield seq[i]".
>>> s[-2:-5:-1] == ''.join(myslice(s, -2, -5, -1))
>>> s[-5:-2:1] == ''.join(myslice(s, -5, -2, 1))
More information about the Python-list