Is there an easier way to express this list slicing?

Chris Mellon arkanes at gmail.com
Thu Nov 30 20:19:59 CET 2006


On 11/30/06, Thomas Ploch <Thomas.Ploch at gmx.net> wrote:
> John Henry schrieb:
> > If I have a list of say, 10 elements and I need to slice it into
> > irregular size list, I would have to create a bunch of temporary
> > variables and then regroup them afterwords, like:
> >
> > # Just for illustration. Alist can be any existing 10 element list
> > a_list=("",)*10
> > (a,b,c1,c2,c3,d1,d2,d3,d4,d5)=a_list
> > alist=(a,)
> > blist=(b,)
> > clist=(c1,c2,c3)
> > dlist=(d2,d3,d4,d5)
> >
> > That obviously work but do I *really* have to do that?
> >
> > BTW: I know you can do:
> > alist=a_list[0]
> > blist=a_list[1]
> > clist=a_list[2:5]
> > dlist=a_list[5:]
> >
> > but I don't see that it's any better.
> >
> > Can I say something to the effect of:
> >
> > (a,b,c[0:2],d[0:5])=a_list    # Obviously this won't work
> >
> > ??
> >
> > I am asking this because I have a section of code that contains *lots*
> > of things like this.  It makes the code very unreadable.
> >
> > Thanks,
> >
>
> Nothing in your code actually __is__ a list. they are all tuples...
> A list is:
> aList = [a,b,c1,c2,c3,d1,d2,d3,d4,d5]
>

True but not relevant, really, he should have said "sequence". But
more importantly,
you don't show what you do with alist, blist,clist,dlist. Without
knowing what the end result is, nobody is going to be able to help you
eliminate the middle steps.

> Thomas
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list