On Wed, May 7, 2008 at 7:46 PM, Ivan Illarionov
<[EMAIL PROTECTED]> wrote:
> On Wed, 07 May 2008 23:29:27 +0000, Yves Dorfsman wrote:
>
> > Is there a way to do:
> > x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
> > x[0,2:6]
> >
> > That would return:
> > [0, 3, 4, 5, 6]
>
> IMHO this notation is confusing.
>
> What's wrong with:
> [0]+x[2:6]
I think Yves meant to return [1, 3, 4, 5, 6], as in Perl's list slicing:
my @x = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
return @x[0, 2..6]; // returns (1, 3, 4, 5, 6)
This isn't incredibly efficient, but it does what you want (I think):
from itertools import chain
class multisliceable(list):
def __getitem__(self, slices):
if isinstance(slices, (slice, int, long)):
return list.__getitem__(self, slices)
else:
return list(chain(*[list.__getitem__(self, s) if isinstance(s, slice)
else [list.__getitem__(self, s)] for s in slices]))
p = open('/etc/passwd')
q = [multisliceable(e.strip().split(':'))[0,2:] for e in p]
-Miles
--
http://mail.python.org/mailman/listinfo/python-list