Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #50453
| References | <4a81b6a6-023e-4d47-9bd0-bbc0516caf6b@googlegroups.com> <c087829a-8e30-4f50-afb9-d28a779932e3@googlegroups.com> |
|---|---|
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
| Date | 2013-07-11 16:14 +0100 |
| Subject | Re: xslice idea | a generator slice |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4590.1373555714.3114.python-list@python.org> (permalink) |
On 11 July 2013 15:54, Russel Walker <russ.pobox@gmail.com> wrote:
> ...oh and here is the class I made for it.
>
> class xslice(object):
> '''
> xslice(seq, start, stop, step) -> generator slice
> '''
>
> def __init__(self, seq, *stop):
Wouldn't it be better if it has the same signature(s) as itertools.islice?
> if len(stop) > 3:
> raise TypeError("xslice takes at most 4 arguments")
> elif len(stop) < 0:
How would len(stop) be negative?
> raise TypeError("xslice requires atleast 2 arguments")
> else:
> start, stop, step = (((0,) + stop[:2])[-2:] + # start, stop
> (stop[2:] + (1,))[:1]) # step
> stop = min(stop, len(seq))
> self._ind = iter(xrange(start, stop, step))
> self._seq = seq
>
> def __iter__(self):
> return self
>
> def next(self):
> return self._seq[self._ind.next()]
>
>
>
> Although now that I think about it, it probably should've just been a simple generator function.
Or you can use itertools.imap:
def xslice(sequence, start_or_stop, *args):
indices = xrange(*slice(start_or_stop, *args).indices(len(sequence)))
return imap(sequence.__getitem__, indices)
Oscar
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
xslice idea | a generator slice Russel Walker <russ.pobox@gmail.com> - 2013-07-11 07:52 -0700
Re: xslice idea | a generator slice Russel Walker <russ.pobox@gmail.com> - 2013-07-11 07:54 -0700
Re: xslice idea | a generator slice Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-07-11 16:14 +0100
Re: xslice idea | a generator slice Russel Walker <russ.pobox@gmail.com> - 2013-07-11 09:21 -0700
Re: xslice idea | a generator slice Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-07-11 17:34 +0100
Re: xslice idea | a generator slice Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-11 12:00 -0600
Re: xslice idea | a generator slice Fábio Santos <fabiosantosart@gmail.com> - 2013-07-11 20:58 +0100
Re: xslice idea | a generator slice Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-11 15:02 -0600
Re: xslice idea | a generator slice Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-11 10:16 -0600
csiph-web