title: Reverse indices of a sorted list
body: <p>I want to return the '<strong><em>reverse</em></strong>' indices of a sorted list. What I mean by that is: I have an unsorted list <code>U</code> and I sort it via <code>S=sorted(U)</code>. Now, I can get the sort indices such that <code>U(idx)=S</code> - but I want <code>S(Ridx) = U</code>.</p>
  
  <p>Here a little example:</p>
  
  <pre><code>U=[5,2,3,1,4]
  
  S=sorted(U)
  
  idx = [U.index(S[i]) for i in range(len(U))]
  &gt;&gt;&gt; idx
  [3, 1, 2, 4, 0]
  
  Ridx = [S.index(U[i]) for i in range(len(U))]
  &gt;&gt;&gt; Ridx
  [4, 1, 2, 0, 3]
  
  &gt;&gt;&gt;[U[idx[i]] for i in range(len(U))] == S
  True
  
  &gt;&gt;&gt;[S[Ridx[i]] for i in range(len(U))] == U
  True
  </code></pre>
  
  <p>What I need is an efficient way to get Ridx.</p>
  
  <p>Thanks!</p>
  
  <hr>
  
  <p>Edit:</p>
  
  <p>All right! I did a little speed test for both of the solutions (@Jon Clements and @Whatang) which answered the question.</p>
  
  <p>The script:</p>
  
  <pre><code>import datetime as DT
  import random
  
  U=[int(1000*random.random()) for i in xrange(pow(10,8))]
  
  S=sorted(U)
  
  idx = sorted(xrange(len(U)), key=U.__getitem__)
  
  T0 = DT.datetime.now()
  ridx = sorted(xrange(len(U)), key=idx.__getitem__)
  print [S[ridx[i]] for i in range(len(U))]==U
  elapsed = DT.datetime.now()-T0
  print str(elapsed)
  
  print '==============='
  T0 = DT.datetime.now()
  ridx = [ y for (x,y) in sorted(zip(idx, range(len(idx)))) ]
  print [S[ridx[i]] for i in range(len(U))]==U
  elapsed = DT.datetime.now()-T0
  print str(elapsed)
  </code></pre>
  
  <p>And the results:</p>
  
  <pre><code>True
  0:02:45.278000
  ===============
  True
  0:06:48.889000
  </code></pre>
  
  <p>Thank you all for the quick and meaningful help!</p>