fast sub list operations

Robin Becker robin at jessikat.fsnet.co.uk
Tue Oct 16 13:36:33 EDT 2001


In article <mailman.1003242015.20647.python-list at python.org>, Rich
Harkins <rharkins at thinkronize.com> writes
>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
....
yes it would work, but what I'm getting at is that there seem to be few
if any fast list methods except the obvious. I would expect that there
should at least be a way to split lists using ranges and I know that
this is done in NumPy so why not in python. map and reduce etc are fast,
but useless without builtin C speed things to go with them.

Skip points out a way to interleave pairs back into the single level

eg

reduce(operator.add,[(1,2),(3,4),(5,6)],()) --> (1, 2, 3, 4, 5, 6)


-- 
Robin Becker



More information about the Python-list mailing list