title: How to load a sparse matrix efficiently?
body: <p>Given a file with this structure:</p>
  
  <ul>
  <li>Single column lines are keys</li>
  <li>Non-zero values of the keys</li>
  </ul>
  
  <p>For example:</p>
  
  <pre><code>abc
  ef 0.85
  kl 0.21
  xyz 0.923
  cldex 
  plax 0.123
  lion -0.831
  </code></pre>
  
  <p><strong>How to create a sparse matrix, <code>csr_matrix</code>?</strong></p>
  
  <pre><code>('abc', 'ef') 0.85
  ('abc', 'kl') 0.21
  ('abc', 'xyz') 0.923
  ('cldex', 'plax') 0.123
  ('cldex', 'lion') -0.31
  </code></pre>
  
  <p>I've tried:</p>
  
  <pre><code>from collections import defaultdict
  
  x = """abc
  ef  0.85
  kl  0.21
  xyz 0.923
  cldex 
  plax    0.123
  lion    -0.831""".split('\n')
  
  k1 = ''
  arr = defaultdict(dict)
  for line in x:
      line = line.strip().split('\t')
      if len(line) == 1:
          k1 = line[0]
      else:
          k2, v = line
          v = float(v)
          arr[k1][k2] = v
  </code></pre>
  
  <p>[out]</p>
  
  <pre><code>&gt;&gt;&gt; arr
  defaultdict(dict,
              {'abc': {'ef': 0.85, 'kl': 0.21, 'xyz': 0.923},
               'cldex': {'plax': 0.123, 'lion': -0.831}})
  </code></pre>
  
  <p>Having the nested dict structure isn't as convenient as the <code>scipy</code> sparse matrix structure. </p>
  
  <p><strong>Is there a way to read the file in the given format above easily into any of the <code>scipy</code> sparse matrix object?</strong> </p>