Skip to content

Commit

Permalink
Added to_dict method to SparseVector
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Jun 9, 2024
1 parent 33eb182 commit 8eaad5c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pgvector/utils/sparsevec.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ def from_dense(cls, value):
def dim(self):
return self._dim

def to_dict(self):
return {i: v for i, v in zip(self._indices, self._values)}

def to_list(self):
vec = [0.0] * self._dim
for i, v in zip(self._indices, self._values):
Expand Down
3 changes: 3 additions & 0 deletions tests/test_sparse_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ def test_repr(self):

def test_dim(self):
assert SparseVector.from_dense([1, 0, 2, 0, 3, 0]).dim() == 6

def test_to_dict(self):
assert SparseVector.from_dense([1, 0, 2, 0, 3, 0]).to_dict() == {0: 1, 2: 2, 4: 3}

0 comments on commit 8eaad5c

Please sign in to comment.