0

Is there a way in python (without numpy) to rank lists? for example:

array1 = [1934,1232,345453,123423423,23423423,23423421]
array = [4,2,7,1,1,2]
ranks = [2,1,3,0,0,1]

Gives me examples only with numpy.

I would primarily like to rank the data and then process the data based on ranks to see which dataelements in array1 contribute to 90% of the highest data in array.

Let me know if you want me to give an example.

Community
  • 1
  • 1
pistal
  • 2,310
  • 13
  • 41
  • 65

1 Answers1

1

IIUC you can build a rank dictionary easily enough, and then loop over the elements of array to find the ranks:

>>> array = [4,2,7,1,1,2]
>>> rankdict = {v: k for k,v in enumerate(sorted(set(array)))}
>>> rankdict
{1: 0, 2: 1, 4: 2, 7: 3}
>>> ranked = [rankdict[a] for a in array]
>>> ranked
[2, 1, 3, 0, 0, 1]

If you want to sort array1 by this ranking, there are several ways to do it. One common one is to build a zipped list and then sort that:

>>> zip(ranked, array)
[(2, 4), (1, 2), (3, 7), (0, 1), (0, 1), (1, 2)]
>>> sorted(zip(ranked, array))
[(0, 1), (0, 1), (1, 2), (1, 2), (2, 4), (3, 7)]
>>> sorted(zip(ranked, array1))
[(0, 23423423), (0, 123423423), (1, 1232), (1, 23423421), (2, 1934), (3, 345453)]
DSM
  • 342,061
  • 65
  • 592
  • 494
  • How do we sort the ranked list and then sort the array1 corresponding to it? – pistal Feb 10 '14 at 03:52
  • and how do we get the top 90% of the data in array? should i sum thru the list and search for 90% and kick the rest out (based on ranks)? – pistal Feb 10 '14 at 04:01
  • @pistal: there are several ways you could define "top 90%"; choose one and try something. – DSM Feb 10 '14 at 04:03
  • Would you suggest running thru the list and searching for top 90%? that's something that strikes me right now. – pistal Feb 10 '14 at 04:05