title: Numpy list comprehension iterating over 2D array
body: <p>I have a bit of code that loads up a long (100k-1mil) set of lines, it has an index in the first column followed by 18 values, for a total of 19 floats per line. This all is put into a numpy array.</p>
  
  <p>I need to do some simple processing on the matrix to keep the index column and get out 1s and 0s depending on conditions of whether values are positive or negative, but the criterion varies as the columns are sequential pairs of values with different reference values.</p>
  
  <p>The code below goes through the columns 2-19 first by evens then odds to check the values, and then creates a temporary list to put into the array I want to have at the end.</p>
  
  <p>I know there's a simpler way to do this, with list comprehension and possibly lambda, but I'm not proficient enough with this to figure it out. So I'm hoping someone can help me reduce the length of this code into something more compact. More efficient would be great too, but I know that the compact methods don't always increase efficiency. It will however help me better understand list comprehension, with and without numpy.</p>
  
  <p>Sample values for reference:</p>
  
  <pre><code> 0.000 72.250 -158.622 86.575 -151.153 85.807 -149.803 84.285 -143.701 77.723 -160.471 96.587 -144.020 75.827 -157.071 87.629 -148.856 100.814 -140.488
  10.000 56.224 -174.351 108.309 -154.148 68.564 -155.721 83.634 -132.836 75.030 -177.971 100.623 -146.616 61.856 -150.885 92.147 -150.124 91.841 -153.112
  20.000 53.357 -153.537 58.190 -160.235 77.575 176.257 93.771 -150.549 77.789 -161.534 103.589 -146.363 73.623 -159.441 99.315 -129.663 92.842 -138.736
  </code></pre>
  
  <p>And here is the code snippet:</p>
  
  <pre><code>datain = numpy.loadtxt(testfile.txt) #load data
  dataout = numpy.zeros(datain.shape) # initialize empty processing array
  
  dataout[:, 0] = datain[:, 0] # assign time values from input data to processing array
  
  dataarray = numpy.zeros(len(datain[0]))
  phit = numpy.zeros((len(dataarray)-1)/2)
  psit = numpy.zeros((len(dataarray)-1)/2)
  
  for i in range(len(datain)):
      dataarray = numpy.copy(datain[i])
      phit[:] = dataarray[1::2]
      psit[:] = dataarray[2::2]
      temp = []
      for j in range(len(phit)):
          if(phit[j] &lt; 0):
              temp.append(1)
          else:
              temp.append(0)
          if(psit[j] &gt; 0):
              temp.append(1)
          else:
              temp.append(0)
      dataout[i][1:] = temp
  </code></pre>
  
  <p>Thanks in advance, I know there's a fair number of questions on these topics here; unfortunately I couldn't find one that helped me get to a solution.</p>