[英]How to use __add__ method to add sparse vectors in python
我被賦予了使用 SparseVec(length) 類中的特殊方法添加兩個稀疏向量的任務。我對特殊方法和方法重載的理解有限,您能否描述一下我在以下代碼中缺少的內容以及方法重載究竟是什么? 我有兩種選擇:使用獨立函數和類方法(OOP)。 我想知道后者的優勢(OOP)。
獨立功能(工作正常)
def SparseVec(numbers):
dic={}
for key,val in enumerate(numbers):
if val:
dic[key]=val
return dic
numbers=[-1,0,9.2,0]
a=SparseVec(numbers)
print(a)
numbers2=[0,1,0,0,0]
b=SparseVec(numbers2)
print(b)
#Adds and merges values with keys in two dictionaries
def merged_dictionaries(a,b):
merged_dict={}
for key in a:
if key in b:
new_value=a[key]+b[key]
else:
new_value=a[key]
merged_dict[key]=new_value
for key in b:
if key not in merged_dict:
merged_dict[key]=b[key]
return merged_dict
c=merged_dictionaries(a,b)
for key, val in c.items(): # SparseVec iterator
print ('c[%d]=%g ' % (key,val))
print(c)
類方法(OOP)-(有缺陷)
#Implements a Sparse vector (vector with many zero values) and adds two sparse vectors
class SparseVec:
#initializes the instance with given length
def __init__(self,length):
self.length=length
self.data={}
def __str__(self):
return 'Dense Vector {}'. format(self.data)
#Returns the length of the vector
def __len__(self):
return len(self.data)
# Returns nonzeros from the given(self) dictionary
def __getitem__(self, item):
return self.data
def __setitem__(self, key, val):
#To set a value by its key
self.data[key]= val
def nonzeros(self):
nonzerodict = {}
for key, val in enumerate(self):
if val:
nonzerodict[key] = val
return nonzerodict
def __add__(self, other):
c = {}
for key in self:
if key in other:
new_value = self[key]+ other[key]
else:
new_value = self[key]
c[key] = new_value
for key in other:
if key not in c:
c[key] = other[key]
return c
a = SparseVec(4)
a[2] = 9.2
a[0] = -1
print(a)
print(a.nonzeros())
b = SparseVec(5)
b[1] = 1
print(b.nonzeros())
c=a+b
print(c)
您需要成對添加每個向量的分量,並返回一個SparseVector
對象:
class SparseVec:
def __init__(self, dimension):
self.dimension = dimension
self.data = {}
def __str__(self):
return 'Sparse Vector {}'. format(self.data)
def __len__(self):
return self.dimension # what matters is the size of the vector, not the length of the stored data
def __getitem__(self, key):
assert isinstance(key, int)
assert 0 <= key < self.dimension, 'the key must be compatible with the vector dimension'
try:
return self.data[key]
except KeyError:
return 0 # must return zero if valid key but no entry
def __setitem__(self, key, val):
assert isinstance(key, int)
assert 0 <= key < self.dimension, 'this vector does not have an appropriate dimension'
if val != 0: # avoid cluttering with zero values
self.data[key] = val
def purge_zeros(self): # <-- resparsifies a vector by purging the zero values
nonzerodict = {}
for key, val in self.data.items():
if val != 0:
nonzerodict[key] = val
self.data = nonzerodict
def __add__(self, other):
assert self.dimension == other.dimension, 'vectors must have the same dimension'
resulting_vector = SparseVec(self.dimension)
c = {k:v for k, v in self.data.items()} # <-- copies self data
for k, v in other.data.items():
try:
c[k] += v
except KeyError:
c[k] = v
resulting_vector.data = c
resulting_vector.purge_zeros()
return resulting_vector
a = SparseVec(4)
b = SparseVec(4)
a.data = {0: 2, 1: 1}
b.data = {0: -2, 1: 2, 2: 4}
print(a + b)
print(a[0], a[1], a[2], a[3])
print(b[0], b[1], b[2], b[3])
a[3] = -3
print(a[0], a[1], a[2], a[3])
Sparse Vector {1: 3, 2: 4}
2 1 0 0
-2 2 4 0
2 1 0 -3
重載類方法本質上是編寫一個通常由 Python 中的默認方法處理的方法,並將其替換為該類的您自己的方法。
這是 Python 3 文檔中的默認添加方法: https : //docs.python.org/3/reference/datamodel.html#emulating-numeric-types
我不確定您為什么為此使用 dict 而不是沒有更多上下文的列表,但以下應該有效:
c = a.add(b)
print(c)
a 是 SparseVec 類的一個實例,因此為了訪問add方法,我們調用 a.add() 並傳入我們希望添加到它的另一個對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.