问题描述
现在,我将vector3值表示为列表.有没有一种方法可以减去其中两个类似的vector3值,例如
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]
我应该使用元组吗?
如果没有一个在这些类型上定义这些操作数,我可以改为定义它吗?
If none of them defines these operands on these types, can I define it instead?
如果没有,我应该创建一个新的vector3类吗?
If not, should I create a new vector3 class?
推荐答案
如果您经常这样做,并且使用不同的操作,则可能应该创建一个类来处理此类情况,或者最好使用类似的库 Numpy .
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.
否则,请查找与列表理解 ="http://docs.python.org/library/functions.html#zip" rel ="noreferrer"> zip 内置函数:
Otherwise, look for list comprehensions used with the zip builtin function:
[a_i - b_i for a_i, b_i in zip(a, b)]
这篇关于用Python减去2个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!