Filling a diagonal matrix based on selectors
<p>I have two lists:</p>

<pre><code>[(1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2)]

[False, False, True, False, False, False]
</code></pre>

<p>The first list represent the row_number, column_number of the matrix. The second list represent the element value. How can I create an efficient loop(or other algorithm) so I end up with a 4 by 4 matrix:</p>

<pre><code>0 0 0 0
0 0 0 0
0 1 0 0
0 0 0 0 
</code></pre>

<p>I would suggest using the <code>sparse</code> library from the <code>scipy</code> module for efficient sparse matrix manipulation. Here is how you would create the desired matrix:</p>

<pre><code>from scipy import sparse
coo = [(1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2)]
data = [False, False, True, False, False, False]

m = sparse.coo_matrix((data,zip(*coo)), shape=(4,4))
print(m)
</code></pre>

<p>Note that there are <a href="http://docs.scipy.org/doc/scipy/reference/sparse.html" rel="nofollow">many</a> other sparse matrix formats (including diagonal) depending on what representation you find most appropriate to create and manipulate it.</p>

python python-3.x