Subtract 2 lists in Python

Right now, I have vector3 values ​​presented as lists. is there a way to subtract 2 from such vector3 values, for example

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

Should I use tuples?

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

If not, should I create a new vector3 class?

+50
python list vector tuples
Feb 10 '09 at 23:57
source share
11 answers

If this is what you often do, and with different operations, you should probably create a class to handle such cases, or better use some library, such as Numpy .

Otherwise, find the list of concepts used with the zip built-in function:

 [a_i - b_i for a_i, b_i in zip(a, b)] 
+94
Feb 11 '09 at 0:00
source share

Here is an alternative to lists. The map iterates through the list (last arguments), doing it simulatively and passes its elements as arguments to the function (first argument). It returns the resulting list.

 map(operator.sub, a, b) 

This code, because it has less syntax (which is more aesthetically pleasing to me) and seems to be 40% faster for lists of length 5 (see bobince's comment). However, any solution will work.

+65
Feb 11 '09 at 0:19
source share

I would recommend NumPy as well

It not only speeds up the execution of vector math, but also has a ton of convenient functions.

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

This is similar to MatLab, but free and more. Here is an example of what you will do

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

Boom.

+9
Feb 11 '09 at 7:59
source share

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 this means.

+8
Feb 11 '09 at 0:17
source share

Check out the NumPy package for python.

+7
Feb 11 '09 at 0:01
source share

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

+4
Feb 10 '09 at 23:59
source share

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( (ab for a,b in zip(self.data, other.data) ) ) Vector(1, 2, 3) - Vector(1, 1, 1) 
+4
Feb 11 '09 at 2:44
source share

If you plan to run more simple firmware, 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 
+3
Feb 11 '09 at 0:03
source share

If you want to get the result in the list:

 list(numpy.array(list1)-numpy.array(list2)) 

if you do not delete the list.

0
Apr 30 '14 at
source share
 import numpy as np a = [2,2,2] b = [1,1,1] np.subtract(a,b) 
0
Nov 09 '17 at 20:38 on
source share

Try the following:

 list(array([1,2,3])-1) 
-one
09 Oct '09 at 12:03
source share



All Articles