views:

3576

answers:

9

Right now I have vector3 values represented as lists. is there a way to subtract 2 of these like vector3 values, like

[2,2,2] - [1,1,1] = [1,1,1]

Should I use tuples?

If none of them defines these operands on these types, can I define it instead?

If not, should I create a new vector3 class?

+2  A: 

If you have two lists called 'a' and 'b', you can do: [m - n for m,n in zip(a,b)]

Andy Mikula
+24  A: 

If this is something you end up doing frequently, and with different operations, you should probably create a class to handle cases like this, or better use some library like Numpy.

Otherwise, look for list comprehensions used with the zip builtin function:

[a - b for a, b in zip(a, b)]
UncleZeiv
+5  A: 

Check out the NumPy package for python.

Richard J. Terrell
+3  A: 

If you plan on performing more than simple one liners, it would be better to implement your own class and override the appropriate operators as they apply to your case.

Taken from Mathematics in Python:

class Vector:

  def __init__(self, data):
    self.data = data

  def __repr__(self):
    return repr(self.data)  

  def __add__(self, other):
    data = []
    for j in range(len(self.data)):
      data.append(self.data[j] + other.data[j])
    return Vector(data)  

x = Vector([1, 2, 3])    
print x + x
codelogic
+1  A: 

If your lists are a and b, you can do:

map(int.__sub__, a, b)

But you probably shouldn't. No one will know what it means.

recursive
+11  A: 

Here's an alternative to list comprehensions. Map iterates through the list(s) (the latter arguments), doing so simulataneously, and passes their elements as arguments to the function (the first arg). It returns the resulting list.

map(operator.sub, a, b)

This code because has less syntax (which is more aesthetic for me), and apparently it's 40% faster for lists of length 5 (see bobince's comment). Still, either solution will work.

Nikhil Chelliah
I usually see list comprehensions being recomemnded over map(), although that may just be because it's cleaner-looking code... not sure about the performance difference, if any.
David Zaslavsky
The map() comes out almost 40% faster for me on Py2.6 for a five-element subtraction. Comprehensions are newer and cleaner where they avoid a lambda, but for mapping existing functions map can still be pretty... especially here where you can leverage the built-in zip.
bobince
+1  A: 

A slightly different Vector class.

class Vector( object ):
    def __init__(self, *data):
        self.data = data
    def __repr__(self):
        return repr(self.data) 
    def __add__(self, other):
        return tuple( (a+b for a,b in zip(self.data, other.data) ) )  
    def __sub__(self, other):
        return tuple( (a-b for a,b in zip(self.data, other.data) ) )

Vector(1, 2, 3) - Vector(1, 1, 1)
S.Lott
+3  A: 

I'd have to recommend NumPy as well

Not only is it faster for doing vector math, but it also has a ton of convenience functions.

If you want something even faster for 1d vectors, try vop

It's similar to MatLab, but free and stuff. Here's an example of what you'd do

from numpy import matrix
a = matrix((2,2,2))
b = matrix((1,1,1))
ret = a - b
print ret
>> [[1 1 1]]

Boom.

mikelikespie
A: 

list(array([1,2,3])-1)

Geert
this works only for numpy, isn't it?
SilentGhost