finding average of a list of coordinates

51 views
Skip to first unread message

s...@weacceptyou.com

unread,
Nov 24, 2016, 1:49:47 PM11/24/16
to Python Programming for Autodesk Maya
hello, this should be a simple one i guess, i have this so far:

avrg_coords=[sum(x) for x in zip (*coord_list)]

but i need to be able to divide the result by len(coord_list) to find the average. Is there a way i can add this into this line, or would i have to do this seperately? maybe there is alternate way to average lists of tuples?


thanks,
Sam

fruity

unread,
Nov 24, 2016, 2:17:37 PM11/24/16
to Python Programming for Autodesk Maya, s...@weacceptyou.com
hi, 

if you want to do it absolutely in one line, maybe something like 

getCentroid = lambda inputs : [sum([a[x] for a in inputs]) / float(len(inputs)) for x in range(3)] if len(inputs) else None

? Then you can use 
getCentroid([[1, 0, 0], [0, 1, 0], [-1, 0, 0]])
for example.
But why doing it in one line, it becomes a bit complicated if other people have to read  your code.

s...@weacceptyou.com

unread,
Nov 24, 2016, 3:51:57 PM11/24/16
to Python Programming for Autodesk Maya, s...@weacceptyou.com
ok, yea its probably best to do it one another line. I just thought there was a command that computed the center of a list of coords easier than constucting it manually.

thanks for the help,

Sam
Reply all
Reply to author
Forward
0 new messages