strategies to assign ranks
In most cases, enumerate a Python standard function is a best tool to make a ranking. But how about tie scores? You may end up with giving different rank for tie scores. And I’m quite sure that will make you and your users dissatisfied. Solution? You are on the right page.
>>> list(enumerate([100, 80, 80, 70]))
[(0, 100), (1, 80), (2, 80), (3, 70)]
This module implements Ranking that looks like enumerate but generates ranks instead of indexes and various strategy to assign ranks to tie scores.
>>> list(Ranking([100, 80, 80, 70])) # same scores have same ranks
[(0, 100), (1, 80), (1, 80), (3, 70)]
This class looks like enumerate but generates a tuple containing a rank and value instead.
>>> scores = [100, 80, 80, 70, None]
>>> list(Ranking(scores))
[(0, 100), (1, 80), (1, 80), (3, 70), (None, None)]
Parameters: |
|
---|
Generates only ranks.
Finds the rank of the value.
Raises ValueError: | |
---|---|
the value isn’t ranked in the ranking |
Ranking follows the strategy function when it assigns ranks to tie scores. Also this module provides most common 5 strategies:
You can also implement your own strategy function. A strategy function has parameters start, a rank of the first tie score; length, a length of tie scores. Then it returns length + 1 for each scores for tie scores and the next rank.
Here’s an example of custom strategy function that assigns no ranks to tie scores:
>>> def exclusive(start, length):
... return [None] * length + [start]
>>> list(Ranking([100, 80, 80, 70], exclusive))
[(0, 100), (None, 80), (None, 80), (1, 70)]
The package is available in PyPI. To install it in your system, use easy_install:
$ easy_install ranking
Or check out developement version:
$ git clone git://github.com/sublee/ranking.git
Released on Mar 4th 2013.
Fixes an error of FRACTIONAL() strategy with multiple tie ranks. Thanks to Yunwon Jeong.
Released on Oct 26th 2012.
Released on Oct 26th 2012.
Released on Oct 26th 2012.
Released on Oct 9th 2012.
Released on Oct 7th 2012.
First public preview release.
This project is licensed under BSD. See LICENSE for the details.
I’m Heungsub Lee, a game developer. Any regarding questions or patches are welcomed.