In [1]:
import numpy as np
from scipy.sparse import csr_matrix, csc_matrix, lil_matrix

In [2]:
l = [[1, 0, 0, 0],
     [0, 2, 0, 0],
     [0, 0, 3, 0],
     [0, 0, 0, 4]]

In [3]:
csr = csr_matrix(l)
csc = csc_matrix(l)
lil = lil_matrix(l)

In [4]:
print(csr[0, :])


  (0, 0)	1

In [5]:
print(csr[0, :].toarray())


[[1 0 0 0]]

In [6]:
print(csr[0].toarray())


[[1 0 0 0]]

In [7]:
print(csr[:, 0])


  (0, 0)	1

In [8]:
print(csr[:, 0].toarray())


[[1]
 [0]
 [0]
 [0]]

In [9]:
print(csr[1:3, 1:3])


  (0, 0)	2
  (1, 1)	3

In [10]:
print(csr[1:3, 1:3].toarray())


[[2 0]
 [0 3]]

In [11]:
print(csr[:, ::2])


  (0, 0)	1
  (2, 1)	3

In [12]:
print(csr[:, ::2].toarray())


[[1 0]
 [0 0]
 [0 3]
 [0 0]]

In [13]:
print(type(csr[0]))


<class 'scipy.sparse.csr.csr_matrix'>

In [14]:
print(type(csc[0]))


<class 'scipy.sparse.csc.csc_matrix'>

In [15]:
print(type(lil[0]))


<class 'scipy.sparse.lil.lil_matrix'>

In [16]:
print(type(csr[:, 0]))


<class 'scipy.sparse.csr.csr_matrix'>

In [17]:
print(type(csc[:, 0]))


<class 'scipy.sparse.csc.csc_matrix'>

In [18]:
print(type(lil[:, 0]))


<class 'scipy.sparse.lil.lil_matrix'>

In [19]:
print(type(csr[1:3, 1:3]))


<class 'scipy.sparse.csr.csr_matrix'>

In [20]:
print(type(csc[1:3, 1:3]))


<class 'scipy.sparse.csc.csc_matrix'>

In [21]:
print(type(lil[1:3, 1:3]))


<class 'scipy.sparse.lil.lil_matrix'>

In [22]:
csr_slice = csr[1:3, 1:3]
csr_slice[0, 0] = 100

In [23]:
print(csr.toarray())


[[1 0 0 0]
 [0 2 0 0]
 [0 0 3 0]
 [0 0 0 4]]

In [24]:
print(csr_slice.toarray())


[[100   0]
 [  0   3]]

In [25]:
csc_slice = csc[1:3, 1:3]
csc_slice[0, 0] = 100

In [26]:
print(csc.toarray())


[[1 0 0 0]
 [0 2 0 0]
 [0 0 3 0]
 [0 0 0 4]]

In [27]:
print(csc_slice.toarray())


[[100   0]
 [  0   3]]

In [28]:
lil_slice = lil[1:3, 1:3]
lil_slice[0, 0] = 100

In [29]:
print(lil.toarray())


[[1 0 0 0]
 [0 2 0 0]
 [0 0 3 0]
 [0 0 0 4]]

In [30]:
print(lil_slice.toarray())


[[100   0]
 [  0   3]]

In [31]:
lil[0] = [10, 20, 30, 40]

In [32]:
print(lil)


  (0, 0)	10
  (0, 1)	20
  (0, 2)	30
  (0, 3)	40
  (1, 1)	2
  (2, 2)	3
  (3, 3)	4

In [33]:
print(lil.toarray())


[[10 20 30 40]
 [ 0  2  0  0]
 [ 0  0  3  0]
 [ 0  0  0  4]]

In [34]:
lil[1:3, 1:3] = np.arange(4).reshape(2, 2) * 100

In [35]:
print(lil)


  (0, 0)	10
  (0, 1)	20
  (0, 2)	30
  (0, 3)	40
  (1, 2)	100
  (2, 1)	200
  (2, 2)	300
  (3, 3)	4

In [36]:
print(lil.toarray())


[[ 10  20  30  40]
 [  0   0 100   0]
 [  0 200 300   0]
 [  0   0   0   4]]

In [37]:
lil[:, 0] = csr[:, 3]

In [38]:
print(lil)


  (0, 1)	20
  (0, 2)	30
  (0, 3)	40
  (1, 2)	100
  (2, 1)	200
  (2, 2)	300
  (3, 0)	4
  (3, 3)	4

In [39]:
print(lil.toarray())


[[  0  20  30  40]
 [  0   0 100   0]
 [  0 200 300   0]
 [  4   0   0   4]]

In [40]:
# lil[1:3, 1:3] = [10, 20, 30, 40]
# ValueError: shape mismatch: objects cannot be broadcast to a single shape

In [41]:
csr[0] = [0, 0, 0, 100]


/usr/local/lib/python3.7/site-packages/scipy/sparse/_index.py:127: SparseEfficiencyWarning: Changing the sparsity structure of a csr_matrix is expensive. lil_matrix is more efficient.
  self._set_arrayXarray(i, j, x)

In [42]:
print(csr)


  (0, 0)	0
  (0, 1)	0
  (0, 2)	0
  (0, 3)	100
  (1, 1)	2
  (2, 2)	3
  (3, 3)	4

In [43]:
print(csr.toarray())


[[  0   0   0 100]
 [  0   2   0   0]
 [  0   0   3   0]
 [  0   0   0   4]]