File tree Expand file tree Collapse file tree 2 files changed +28
-0
lines changed Expand file tree Collapse file tree 2 files changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -253,6 +253,29 @@ def nnz(self):
253
253
def nbytes (self ):
254
254
return self .data .nbytes + self .coords .nbytes
255
255
256
+ def __len__ (self ):
257
+ """
258
+ Get "length" of array, which is by definition the size of the first
259
+ dimension.
260
+
261
+ Returns
262
+ -------
263
+ int
264
+ The size of the first dimension.
265
+
266
+ See Also
267
+ --------
268
+ numpy.ndarray.__len__ : Numpy equivalent property.
269
+
270
+ Examples
271
+ --------
272
+ >>> x = np.zeros((10, 10))
273
+ >>> s = COO.from_numpy(x)
274
+ >>> len(s)
275
+ 10
276
+ """
277
+ return self .shape [0 ]
278
+
256
279
def __sizeof__ (self ):
257
280
return self .nbytes
258
281
Original file line number Diff line number Diff line change @@ -875,3 +875,8 @@ def test_scalar_shape_construction():
875
875
s = COO (coords , x , shape = 5 )
876
876
877
877
assert_eq (x , s )
878
+
879
+
880
+ def test_len ():
881
+ s = sparse .random ((20 , 30 , 40 ))
882
+ assert len (s ) == 20
You can’t perform that action at this time.
0 commit comments