slicing return iter?
weasley_wx at sina.com
Sun Oct 18 07:02:27 CEST 2009
On 10月18日, 上午9时09分, Raymond Hettinger <pyt... at rcn.com> wrote:
> > > > sometimes I want to iterate a part of a sequence. but don't want to
> > > > copy it. i.e.
> . . .
> > I had checked it for serval times. maybe it's my inattention :-(. but
> > what i could find the nearest thing is itertools.islice. but it can't
> > process negative index -- that's supported by slice. so I need
> > something, bind object with slice, and return a iter. I can find
> > anything like it...:-(
> 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 :-(
Terry Reedy is right. since a range (or xrange or slice etc.) don't
have a length, so it can't support a negative index. so the best way
to do it is that binding a range with a object. and return a iter.
I think, why standard library didn't have anything like that, that
will be very useful. maybe we should have a builtin functoin
itertools.bslice (stands for bind slice)...
More information about the Python-list