fast sub list operations

Rich Harkins rharkins at thinkronize.com
Tue Oct 16 10:19:39 EDT 2001


Would this work for you?

coords=[x1,y1,x2,y2,x3,y3,...]
final=[(coords[i],coords[i+1]) for i in xrange(0,len(coords),2)]
final will now be [(x1,y1),(x2,y2),(x3,y3),...]

Wouldn't be terribly fast but it should do the trick.  Use range instead of
xrange if you know coords will not be very big.  You will get an IndexError
if the length of coords is not a multiple of two.

Rich

> -----Original Message-----
> From: python-list-admin at python.org
> [mailto:python-list-admin at python.org]On Behalf Of Robin Becker
> Sent: Tuesday, October 16, 2001 9:39 AM
> To: Skip Montanaro
> Cc: python-list at python.org
> Subject: Re: fast sub list operations
>
>
> In message <15308.11525.102304.624520 at beluga.mojam.com>, Skip Montanaro
> <skip at pobox.com> writes
> >
> >
> ....
> >
> >I think most people will point you to NumPy.
> >
> >    Robin> ... I don't seem to have an interlace to get back to
> the original
> >    Robin> list format.
> >
> >Check out the zip builtin function:
> >
> >    >>> zip([1,2,3],[7,8,9])
> >    [(1, 7), (2, 8), (3, 9)]
> >
>
> I know about NumPy and like it a lot, but it's a bit heavyweight for
> what we need. I also know about zip and apart from there being no unzip
> it doesn't join up my list which should look like [1,7,2,8,3,9].
> --
> Robin Becker
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>





More information about the Python-list mailing list