title: ranking tuples with ties
body: <p>I am writing an extension of the Wilcoxon rank-sums test, which requires that I write the basic function of this test first  This also means I can not use SciPy for this exercise. </p>
  
  <p>I have the basic skeleton code there, but I am having trouble averaging the rank of ties.  Here is my code:</p>
  
  <pre><code>#read in data
  m1 = [0,0,0,0,0,2,3,3,3,4,4,5,6,10,10,10,11,12,15,15,15,20,22,25,25,27,30]
  w1 = [0,0,0,0,0,0,1,3,3,3,3,7,8,8,19,20,27,30]
  
  #convert to tuples, incl where they came from
  m1t = []
  for m in m1:
      m1t.append((m, "m1"))
  w1t = []
  for w in w1:
      w1t.append((w, "w1"))
  
  all1t = m1t + w1t #combine
  
  all1ts = sorted(all1t, key=lambda tup: tup[0]) #sort
  
  all1tsr = [row+(i,) for i,row in enumerate(all1ts,0)] #rank
  
  #revert to back to original grouping
  m1r = [i for i in all1tsr if i[1]=="m1"]
  w1r = [i for i in all1tsr if i[1]=="w1"]
  </code></pre>
  
  <p>and here's the current output:</p>
  
  <pre><code>&gt;&gt;&gt; all1tsr[:15]
  [(0, 'm1', 0),
   (0, 'm1', 1),
   (0, 'm1', 2),
   (0, 'm1', 3),
   (0, 'm1', 4),
   (0, 'w1', 5),
   (0, 'w1', 6),
   (0, 'w1', 7),
   (0, 'w1', 8),
   (0, 'w1', 9),
   (0, 'w1', 10),
   (1, 'w1', 11),
   (2, 'm1', 12),
   (3, 'm1', 13),
   (3, 'm1', 14)]
  </code></pre>
  
  <p>Element 1 of eachtuple is the value by which they are sorted, element 2 is just an identifier, and element 3 is the rank when sorted by element 1.  There are 10 observations with "0" as element 1, and right now they are all assigned ascending ranks but I want to somehow average these ranks (assign all to have a rank of 5).  </p>
  
  <p>In other words I want this:</p>
  
  <pre><code>[(0, 'm1', 5),
   (0, 'm1', 5),
   (0, 'm1', 5),
   (0, 'm1', 5),
   (0, 'm1', 5),
   (0, 'w1', 5),
   (0, 'w1', 5),
   (0, 'w1', 5),
   (0, 'w1', 5),
   (0, 'w1', 5),
   (0, 'w1', 5),
   (1, 'w1', 11),
   (2, 'm1', 12),
   (3, 'm1', 13.5),
   (3, 'm1', 13.5)]
  </code></pre>
  
  <p>All feedback is welcomed, thanks</p>