title: How do I go about simultaneously sorting a list of lists while filtering out certain values?
body: <p>I have an ascii file of three columns and I want to sort and filter the file based on the third column. That is, I want to arrange the third column from smallest to largest and take out all values above, say, 8.0. I know how to do both of these things if it were done directly for one list. However, I don't know how to apply the changes I made for the third column to all the columns, since each row is linked to one another. </p>
  
  <p>Small side question: I have unpacked the ascii file as so:</p>
  
  <pre><code>f=open('textfile.dat',"r")
  lines=f.readlines()
  result=[]
  for x in lines:
      result.append(x.split('\n')[0])
  
  a = []
  b = []
  c = []    
  for w in result:
      ra.append(w.split()[0])
  for x in result:
      dec.append(x.split()[1])
  for y in result:
      mag.append(y.split()[2])
  
  p=0
  for i in a:
      a[p] = float(i)
      p= p+1    
  q=0
  for j in b:
      b[q] = float(j)
      q= q+1    
  r=0
  for k in c:
      c[r] = float(k)
      r= r+1
  
  test=[a,b,c]
  </code></pre>
  
  <p>Which seems unnecessarily tedious to say the least. Is there any way to do this more compactly? It's simply an ascii file with four columns. Though they are numbers, they're being read as 'str' and so I have to change it back to floats. </p>
  
  <p>Sorting seems to just be using a transposer then sorting according to columns, which solves my first question.</p>
  
  <pre><code>tset = zip(*test)
  tset.sort(key = lambda x: x[2])
  sorttest = zip(*tset)
  </code></pre>
  
  <p>However, if I wanted to get rid of all the numbers in the third column above, say, 8.0, I would do something like:</p>
  
  <pre><code>testrange[2] = [i for i in sorttest[2] if i &lt;= 8.0]
  </code></pre>
  
  <p>However, that only applies to one row. I can't for example, do something like </p>
  
  <pre><code>testrange[0] = [i*1.0 for i in sorttest[2] if i &lt;= 8.0]
  testrange[1] = [i*1.0 for i in sorttest[2] if i &lt;= 8.0]
  </code></pre>
  
  <p>Thus, how do I go about simultaneously sorting a list of lists while filtering out certain values? </p>
  
  <p>Edit:</p>
  
  <pre><code>Sample input:
   [5,12,3,7,90]
   [9,1,63,23,8]
   [73,11,8,2,5]
  
  Sample output:
   [7,90,3,12]
   [23,8,63,1]
   [2,5,8,11]
  </code></pre>