在 Python 中减去 2 个列表 [英] Subtracting 2 lists in Python

查看:25
本文介绍了在 Python 中减去 2 个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我将 vector3 值表示为列表.有没有办法减去其中的 2 个,比如 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 个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆