title: In coo_matrix (scipy sparse matrix), how can I select and return "random column index" in each row excluding non-zero indexes in that row?
body: <p>I am new to Python and this may seem to be pretty naive. I am working with coo_matrix (scipy sparse matrix) <strong>M</strong> and want to return triplets of M:</p>
  
  <pre><code>row_index,column_index,random_index
  </code></pre>
  
  <p>with following conditions:</p>
  
  <ol>
  <li>row_index,column_index must point to a non-zero entry of M</li>
  <li>row_index,column_index corresponding to all non-zero entries should be returned</li>
  <li>For each pair of row_index,column_index, random_index should be returned</li>
  <li>random_index should be in the range of [0,total_num_columns] of M</li>
  <li>random_index should not be in the set of all the column indices which point to some non-zero value for that row_index</li>
  </ol>
  
  <p>I could come up with following function:</p>
  
  <pre><code> def get_triplets(M):
  return M.row, M.col, np.random.randint(M.shape[1], size=len(M.row))
  </code></pre>
  
  <p>Everything works fine in the above function except last entry of triplet - <strong>random_index</strong> is not in desired way. It is not guaranteed that random index will not lie in set of non-zero indices for that row. In other words, condition number (5) is not satisfied</p>
  
  <p>Is there an efficient way of returning triplets meeting all the above mentioned 5 conditions. Hoping that I could make myself clear and I appreciate any help in solving my problem</p>
  
  <p>Thanks</p>