可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
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_i - b_i for a_i, b_i in zip(a, b)]
回答2:
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.
回答3:
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.
回答4:
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.
回答5:
If you have two lists called 'a' and 'b', you can do: [m - n for m,n in zip(a,b)]
回答6:
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)
回答7:
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
回答8:
import numpy as np
a = [2,2,2]
b = [1,1,1]
np.subtract(a,b)
回答9:
For the one who used to code on Pycharm, it also revives others as well.
import operator
Arr1=[1,2,3,45]
Arr2=[3,4,56,78]
print(list(map(operator.sub,Arr1,Arr2)))
回答10:
The combination of map
and lambda
functions in Python is a good solution for this kind of problem:
a = [2,2,2]
b = [1,1,1]
map(lambda x,y: x-y, a,b)
zip
function is another good choice, as demonstrated by @UncleZeiv
回答11:
arr1=[1,2,3]
arr2=[2,1,3]
ls=[arr2-arr1 for arr1,arr2 in zip(arr1,arr2)]
print(ls)
>>[1,-1,0]
回答12:
Try this:
list(array([1,2,3])-1)