FW: fast sub list operations

Alves, Carlos Alberto - Coelce calves at coelce.com.br
Tue Oct 16 10:48:58 EDT 2001


-----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
-- 
http://mail.python.org/mailman/listinfo/python-list
****************************************************************************
****************************************************************************
******************************************
What do you think of this?

1 - Define the list of coordenates like this

X=[(x1,y1),(x2,y2),...,(xn,yn)]

and operator like this

oper=[(v,w)]

2 - Then, define a function 

def add_list_tuples(lista,listb):
	import operator
	result=[]
	for i in range(len(a)):
		p=operator.add(a[i][0],b[i][0])
		q=operator.add(a[i][1],b[i][1])
		result.append(tuple((p,q)))
	return result

3 - Then, all you need to do is to apply

map(add_list_tuples,X,n*oper)

to get what you want
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20011016/4df848ab/attachment.html>


More information about the Python-list mailing list