FW: fast sub list operations
Dale Strickland-Clark
dale at riverhall.NOTHANKS.co.uk
Tue Oct 16 11:43:54 EDT 2001
"Alves, Carlos Alberto - Coelce" <calves at coelce.com.br> wrote:
>-----Original Message-----
>From: Robin Becker [mailto:robin at jessikat.fsnet.co.uk]
>Sent: Tuesday, October 16, 2001 8:34 AM
>To: python-list at python.org
>Subject: fast sub list operations
>
>
>I constantly miss some nice way to subset lists. As an example suppose I
>have a list of x y coordinates eg
>
> [x0,y0,x1,y1,.....]
>
>and wish to perform the operation x->x+v, y->y+w for the co-ordinates in
>the list I don't seem to be able to do this fast using map. Even if I
>had a way to slice the list nicely into
>
>X=[x0,x1,.....x(n-1)] & Y=[y0,y1,.....,y(n-1)]
>
>so that I can do map(operator.add,X,n*[v]) to perform the arithmetic
>quickly I don't seem to have an interlace to get back to the original
>list format.
>
>How can these kinds of operations be performed quickly in current python
>and what if any new features would python require to do them best?
>--
>Robin Becker
A simple class should help manipulate these lists:
class pairs:
def __init__(self, list):
self.list = list
def __getitem__(self, index):
if index * 2 >= len(self.list):
raise IndexError
return self.list[index * 2: index * 2 + 2]
l = pairs(range(20))
print 'Pairs', [[x, y] for x,y in l]
print 'Odds', [y for x,y in l]
print 'Evens', [x for x,y in l]
print 'Doubled evens', [x*2 for x,y in l]
--
Dale Strickland-Clark
Riverhall Systems Ltd
More information about the Python-list
mailing list