[英]Subtracting 2 lists in Python
現在我將 vector3 值表示為列表。 有沒有辦法減去其中的 2 個,例如 vector3 值,例如
[2,2,2] - [1,1,1] = [1,1,1]
我應該使用元組嗎?
如果他們都沒有在這些類型上定義這些操作數,我可以定義它嗎?
如果沒有,我應該創建一個新的 vector3 類嗎?
這是列表推導式的替代方法。 Map 遍歷列表(后一個參數),同時進行,並將它們的元素作為參數傳遞給函數(第一個參數)。 它返回結果列表。
import operator
map(operator.sub, a, b)
這段代碼因為語法較少(這對我來說更美觀),顯然長度為 5 的列表快了 40%(參見 bobince 的評論)。 盡管如此,任何一種解決方案都將奏效。
如果您的列表是 a 和 b,您可以執行以下操作:
map(int.__sub__, a, b)
但你可能不應該。 沒有人會知道這意味着什么。
import numpy as np
a = [2,2,2]
b = [1,1,1]
np.subtract(a,b)
如果你有兩個名為“a”和“b”的列表,你可以這樣做: [m - n for m,n in zip(a,b)]
一個稍微不同的 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)
對於曾經在 Pycharm 上編碼的人來說,它也讓其他人重振旗鼓。
import operator
Arr1=[1,2,3,45]
Arr2=[3,4,56,78]
print(list(map(operator.sub,Arr1,Arr2)))
如果您計划執行多個簡單的襯墊,最好實現您自己的類並覆蓋適用於您的案例的適當運算符。
取自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
Python 中map
和lambda
函數的結合,很好地解決了這類問題:
a = [2,2,2]
b = [1,1,1]
map(lambda x,y: x-y, a,b)
zip
函數是另一個不錯的選擇,如@UncleZeiv 所示
arr1=[1,2,3]
arr2=[2,1,3]
ls=[arr2-arr1 for arr1,arr2 in zip(arr1,arr2)]
print(ls)
>>[1,-1,0]
這個答案展示了如何編寫“正常/易於理解”的pythonic代碼。
我建議不要使用zip
因為並不是每個人都知道它。
解決方案使用列表推導式和常見的內置函數。
a = [2, 2, 2]
b = [1, 1, 1]
result = [a[i] - b[i] for i in range(len(a))]
推薦,因為它只使用 Python 中最基本的功能
a = [2, 2, 2]
b = [1, 1, 1]
result = [x - b[i] for i, x in enumerate(a)]
a = [2, 2, 2]
b = [1, 1, 1]
result = list(map(lambda x, y: x - y, a, b))
已經提出了許多解決方案。
如果對速度感興趣,這里是關於速度(從最快到最慢)的不同解決方案的回顧
import timeit
import operator
a = [2,2,2]
b = [1,1,1] # we want to obtain c = [2,2,2] - [1,1,1] = [1,1,1
%timeit map(operator.sub, a, b)
176 ns ± 7.18 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%timeit map(int.__sub__, a, b)
179 ns ± 4.95 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%timeit map(lambda x,y: x-y, a,b)
189 ns ± 8.1 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%timeit [a_i - b_i for a_i, b_i in zip(a, b)]
421 ns ± 18.4 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%timeit [x - b[i] for i, x in enumerate(a)]
452 ns ± 17.2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each
%timeit [a[i] - b[i] for i in range(len(a))]
530 ns ± 16.7 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%timeit list(map(lambda x, y: x - y, a, b))
546 ns ± 16.1 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%timeit np.subtract(a,b)
2.68 µs ± 80.9 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%timeit list(np.array(a) - np.array(b))
2.82 µs ± 113 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%timeit np.matrix(a) - np.matrix(b)
12.3 µs ± 437 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
使用map
顯然是最快的。 令人驚訝的是, numpy
是最慢的。 事實證明,首先將列表a
和b
轉換為numpy
數組的成本是一個瓶頸,它超過了矢量化帶來的任何效率收益。
%timeit a = np.array([2,2,2]); b=np.array([1,1,1])
1.55 µs ± 54.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
a = np.array([2,2,2])
b = np.array([1,1,1])
%timeit a - b
417 ns ± 12.8 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
使用 for 循環
a = [3,5,6]
b = [3,7,2]
c = []
for i in range(len(a)):
c.append(a[i] - b[i])
print(c)
輸出 [0, -2, 4]
好簡單
list1=[1,2,3,4,5]
list2=[1,2]
list3=[]
# print(list1-list2)
for element in list1:
if element not in list2:
list3.append(element)
print(list3)
嘗試這個:
list(array([1,2,3])-1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.