title: Creating a list of nearest neighbors using numpy array
body: <p>Based on a cutoff distance I want to create a list of points which are neighbors for a given point. I'm stuck how to add the ids of the points to a numpy array.</p>
  
  <p>I read the 'coordinate' in a numpy array like below:</p>
  
  <pre><code>ID x y z
  
  1. 1 1 0.000000 3.078000 0.554000
  2. 2 0.000000 3.078000 2.170000
  3. 3 2.666000 1.539000 0.554000
  4. 4 2.666000 1.539000 2.170000
  5. 5 1.774000 0.000000 3.324000
  6. 6 2.712000 0.000000 7.207000
  </code></pre>
  
  <p>and so on...</p>
  
  <p>I want the output list like this</p>
  
  <pre><code>ID neighbor1 neighbor2 ....neighbor5
  
  1. 1 4 5 6 8 
  2. 2 1 4 6 n/a
  3. 3 5 1 n/a 2
  4. etc.
  </code></pre>
  
  <p>so each position (1,2,3,4...so on) can have a maximum of 5 neighbors, if less than 5 I want to put n/a there. I have the following code so far.</p>
  
  <pre><code>#neighborlist
  
  flist = glob.glob(filename)
  
  for f in flist:
      load = np.loadtxt(f, usecols=(2,3,4))
      coordinate=np.array(load)
  
  s = (len(coordinate),6)
  
  Nlist = np.zeros(s)
  
  rcut = 2.5 #cutoff distance for neighbors
  
  for f in flist:
      load = np.loadtxt(f, usecols=(2,3,4))
      coordinate=np.array(load)
  
  for i in range(len(coordinate)):
      idx = [i,]
  
      for j in range(len(coordinate)):
          if np.linalg.norm(coordinate[i]-coordinate[j]) &lt; rcut and np.linalg.norm(coordinate[i]-coordinate[j]) &gt; 0.1:
              idx.append(j)            
          else: 
              idx = idx
      while len(idx)&lt;6:
         idx.append('n/a')
      print idx 
      Nlist[i,:]=idx
  
      print Nlist
  </code></pre>
  
  <p>for this i'm getting:</p>
  
  <blockquote>
    <p>ValueError: could not convert string to float: n/a</p>
  </blockquote>
  
  <p>as I have fixed the <code>Nlist</code> array size to <code>(len(data),6)</code> it cannot copy <code>Nlist[i,:]=idx</code> when the neighbors are less than 5 (the first element is the point itself). In this case is there a way to declare <code>Nlist</code> dynamically or make its size variable? I know its a tough question but there has to be a way around this problem. Thanks in advance.</p>