title: Iterating two arrays, without nditer, in numpy?
body: <p>Consider a specification of <code>numpy</code> arrays, typical for specifying <code>matplotlib</code> plotting data:</p>
  
  <pre><code>t = np.arange(0.0,1.5,0.25)
  s = np.sin(2*np.pi*t) 
  </code></pre>
  
  <p>Basically, this stores the <code>x</code> coordinates of our <code>(x,y)</code> data points in the array <code>t</code>; and the resulting <code>y</code> coordinates (result of y=f(x), in this case <code>sin(x)</code>) in the array <code>s</code>. Then, it is very convenient to use the <code>numpy.nditer</code> function to obtain consecutive pairs of entries in <code>t</code> and <code>s</code>, representing the <code>(x,y)</code> coordinate of a data point, as in:</p>
  
  <pre><code>for x, y in np.nditer([t,s]):
    print("xy: %f:%f" % (x,y))
  </code></pre>
  
  <p>So, I'm trying the following snippet as <code>test.py</code>:</p>
  
  <pre><code>import numpy as np
  print("numpy version {0}".format(np.__version__))
  t = np.arange(0.0,1.5,0.25)   ; print("t", ["%+.2e"%i for i in t])
  s = np.sin(2*np.pi*t)         ; print("s", ["%+.2e"%i for i in s])
  print("i", ["% 9d"%i for i in range(0, len(t))])
  for x, y in np.nditer([t,s]):
    print("xy: %f:%f" % (x,y))
  </code></pre>
  
  <p>... and the results are:</p>
  
  <pre><code>$ python3.2 test.py 
  numpy version 1.7.0
  t ['+0.00e+00', '+2.50e-01', '+5.00e-01', '+7.50e-01', '+1.00e+00', '+1.25e+00']
  s ['+0.00e+00', '+1.00e+00', '+1.22e-16', '-1.00e+00', '-2.45e-16', '+1.00e+00']
  i ['        0', '        1', '        2', '        3', '        4', '        5']
  xy: 0.000000:0.000000
  xy: 0.250000:1.000000
  xy: 0.500000:0.000000
  xy: 0.750000:-1.000000
  xy: 1.000000:-0.000000
  xy: 1.250000:1.000000
  
  $ python2.7 test.py 
  numpy version 1.5.1
  ('t', ['+0.00e+00', '+2.50e-01', '+5.00e-01', '+7.50e-01', '+1.00e+00', '+1.25e+00'])
  ('s', ['+0.00e+00', '+1.00e+00', '+1.22e-16', '-1.00e+00', '-2.45e-16', '+1.00e+00'])
  ('i', ['        0', '        1', '        2', '        3', '        4', '        5'])
  Traceback (most recent call last):
    File "test.py", line 10, in &lt;module&gt;
      for x, y in np.nditer([t,s]):
  AttributeError: 'module' object has no attribute 'nditer'
  </code></pre>
  
  <p>Ah - it turns out, that <a href="http://docs.scipy.org/doc/numpy/reference/arrays.nditer.html#arrays-nditer" rel="noreferrer">the iterator object nditer, introduced in NumPy 1.6</a>, is not available in the <code>numpy</code> version of my Python 2.7 installation.</p>
  
  <p>So, as I'd like to support that particular version too, I'd need to find a way working for older <code>numpy</code> - but I'd still like the convenience of just specifying <code>for x,y in somearray</code>, and getting the coordinates directly in the loop. </p>
  
  <p>After some messing about with <code>numpy</code> documentation, I came up with this <code>getXyIter</code> function: </p>
  
  <pre><code>import numpy as np
  print("numpy version {0}".format(np.__version__))
  t = np.arange(0.0,1.5,0.25)   ; print("t", ["%+.2e"%i for i in t])
  s = np.sin(2*np.pi*t)         ; print("s", ["%+.2e"%i for i in s])
  print("i", ["% 9d"%i for i in range(0, len(t))])
  
  def getXyIter(inarr):
    if np.__version__ &gt;= "1.6.0":
      return np.nditer(inarr.tolist())
    else:
      dimensions = inarr.shape
      xlen = dimensions[1]
      xinds = np.arange(0, xlen, 1)
      return np.transpose(np.take(inarr, xinds, axis=1))
  
  for x, y in getXyIter(np.array([t,s])):
    print("xyIt: %f:%f" % (x,y))
  
  for x, y in np.nditer([t,s]):
    print("xynd: %f:%f" % (x,y))
  </code></pre>
  
  <p>... which seems to work fine</p>
  
  <pre><code>$ python2.7 test.py 
  numpy version 1.5.1
  ('t', ['+0.00e+00', '+2.50e-01', '+5.00e-01', '+7.50e-01', '+1.00e+00', '+1.25e+00'])
  ('s', ['+0.00e+00', '+1.00e+00', '+1.22e-16', '-1.00e+00', '-2.45e-16', '+1.00e+00'])
  ('i', ['        0', '        1', '        2', '        3', '        4', '        5'])
  xyIt: 0.000000:0.000000
  xyIt: 0.250000:1.000000
  xyIt: 0.500000:0.000000
  xyIt: 0.750000:-1.000000
  xyIt: 1.000000:-0.000000
  xyIt: 1.250000:1.000000
  Traceback (most recent call last):
    File "test.py", line 23, in &lt;module&gt;
      for x, y in np.nditer([t,s]):
  AttributeError: 'module' object has no attribute 'nditer'
  $ python3.2 test.py 
  numpy version 1.7.0
  t ['+0.00e+00', '+2.50e-01', '+5.00e-01', '+7.50e-01', '+1.00e+00', '+1.25e+00']
  s ['+0.00e+00', '+1.00e+00', '+1.22e-16', '-1.00e+00', '-2.45e-16', '+1.00e+00']
  i ['        0', '        1', '        2', '        3', '        4', '        5']
  xyIt: 0.000000:0.000000
  xyIt: 0.250000:1.000000
  xyIt: 0.500000:0.000000
  xyIt: 0.750000:-1.000000
  xyIt: 1.000000:-0.000000
  xyIt: 1.250000:1.000000
  xynd: 0.000000:0.000000
  xynd: 0.250000:1.000000
  xynd: 0.500000:0.000000
  xynd: 0.750000:-1.000000
  xynd: 1.000000:-0.000000
  xynd: 1.250000:1.000000
  </code></pre>
  
  <p>My question is - is this the way, this kind of iteration is supposed to be done, in versions of numpy &lt; 1.6.0?</p>