在Python中减去2个列表

Joa*_*nge 75 python tuples list vector

现在我将vector3值表示为列表.有没有办法减去其中2个像vector3值,比如

[2,2,2] - [1,1,1] = [1,1,1]
Run Code Online (Sandbox Code Playgroud)

我应该使用元组吗?

如果它们都没有在这些类型上定义这些操作数,我可以改为定义它吗?

如果没有,我应该创建一个新的vector3类吗?

Unc*_*eiv 120

如果这是你最终经常做的事情,并且使用不同的操作,你应该创建一个类来处理这样的情况,或者更好地使用像Numpy这样的库.

否则,查找与zip内置函数一起使用的列表推导:

[a_i - b_i for a_i, b_i in zip(a, b)]
Run Code Online (Sandbox Code Playgroud)

  • `[i - j for i,j in zip(a,b)]`更具可读性,并且不会用a和b中的项替换a和b. (61认同)

Nik*_*iah 81

这是列表推导的替代方案.Map迭代遍历列表(后面的参数),同时这样做,并将它们的元素作为参数传递给函数(第一个arg).它返回结果列表.

map(operator.sub, a, b)
Run Code Online (Sandbox Code Playgroud)

这段代码因为语法较少(对我来说更美观),而且对于长度为5的列表显然要快40%(参见bobince的评论).不过,任何一种解决方案都可行

  • 需要'import operator'条款; int .__ sub__做得更好)) (5认同)
  • 对于我来说,对于我来说,对于五元素的减法,map()的速度快了近40%.理解是更新更清洁,他们避免使用lambda,但是为了映射现有的功能,地图仍然可以很漂亮...特别是在这里您可以利用内置的zip. (2认同)

rec*_*ive 13

如果您的列表是a和b,则可以执行以下操作:

map(int.__sub__, a, b)
Run Code Online (Sandbox Code Playgroud)

但你可能不应该这样做.没有人会知道这意味着什么.

  • 自己用花车撞到了这个。在这种情况下,`map(float.__sub__, a, b)` 有效。谢谢你的提示! (2认同)

mik*_*pie 9

我不得不建议与NumPy以及

它不仅可以更快地进行矢量数学运算,而且还具有大量的便利功能.

如果你想要1d矢量更快的东西,试试vop

它类似于MatLab,但是免费和东西.这是你要做的一个例子

from numpy import matrix
a = matrix((2,2,2))
b = matrix((1,1,1))
ret = a - b
print ret
>> [[1 1 1]]
Run Code Online (Sandbox Code Playgroud)

繁荣.


use*_*711 8

import numpy as np
a = [2,2,2]
b = [1,1,1]
np.subtract(a,b)
Run Code Online (Sandbox Code Playgroud)


And*_*ula 6

如果您有两个名为"a"和"b"的列表,则可以执行以下操作: [m - n for m,n in zip(a,b)]


Jaf*_*ado 6

已经提出了许多解决方案。

\n

如果对速度感兴趣,这里是对速度方面不同解决方案的回顾(从最快到最慢)

\n
import timeit\nimport operator\n\na = [2,2,2]\nb = [1,1,1]  # we want to obtain c = [2,2,2] - [1,1,1] = [1,1,1\n\n%timeit map(operator.sub, a, b)\n176 ns \xc2\xb1 7.18 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 1000000 loops each)\n\n%timeit map(int.__sub__, a, b)\n179 ns \xc2\xb1 4.95 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 1000000 loops each)\n\n%timeit map(lambda x,y: x-y, a,b)\n189 ns \xc2\xb1 8.1 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 1000000 loops each)\n\n%timeit [a_i - b_i for a_i, b_i in zip(a, b)]\n421 ns \xc2\xb1 18.4 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 1000000 loops each)\n\n%timeit [x - b[i] for i, x in enumerate(a)]\n452 ns \xc2\xb1 17.2 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 1000000 loops each\n\n%timeit [a[i] - b[i] for i in range(len(a))]\n530 ns \xc2\xb1 16.7 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 1000000 loops each)\n\n%timeit list(map(lambda x, y: x - y, a, b))\n546 ns \xc2\xb1 16.1 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 1000000 loops each)\n\n%timeit np.subtract(a,b)\n2.68 \xc2\xb5s \xc2\xb1 80.9 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 100000 loops each)\n\n%timeit list(np.array(a) - np.array(b))\n2.82 \xc2\xb5s \xc2\xb1 113 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 100000 loops each)\n\n%timeit np.matrix(a) - np.matrix(b)\n12.3 \xc2\xb5s \xc2\xb1 437 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 100000 loops each)\n
Run Code Online (Sandbox Code Playgroud)\n

使用map显然是最快的。\n令人惊讶的是,numpy是最慢的。事实证明,首先将列表转换为数组的成本ab一个numpy瓶颈,超过了矢量化带来的任何效率提升。

\n
%timeit a = np.array([2,2,2]); b=np.array([1,1,1])\n1.55 \xc2\xb5s \xc2\xb1 54.9 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 1000000 loops each)\n\na = np.array([2,2,2])\nb = np.array([1,1,1])\n%timeit a - b\n417 ns \xc2\xb1 12.8 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 1000000 loops each)\n
Run Code Online (Sandbox Code Playgroud)\n


S.L*_*ott 5

一个稍微不同的 Vector 类。

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)
Run Code Online (Sandbox Code Playgroud)