Skip to main content

Strategies to assign ranks

Project description

PyRanking

Strategies to assign ranks

Coverage Status License

Strategies to assign ranks

In most cases, a sorting function is a best tool to make a ranking. But how about tie scores? You may end up with giving different ranks for tie scores. And I'm quite sure that will make your users dissatisfied.

Solution? You are on the right page.

This module provides various ranking strategies to assign correct ranks to tie scores. It comes with 5 most common strategies: competition, modified-competition, dense, ordinal, and fractional.

Installation

python -m pip install pyranking

Usage

Suppose we want to rank an array of computer languages by the year they were introduced.

languages = [
  { "name": 'Javascript', "year": 1995 },
  { "name": 'Java', "year": 1995 },
  { "name": 'C#', "year": 2001 },
  { "name": 'Groovy', "year": 2003 },
  { "name": 'Scala', "year": 2003 },
  { "name": 'Go', "year": 2009 },
];

We need to provide a function defining the sorting criteria. The function must accept one entry of the array and returns a value to rank on (the year in this case). Note the returned value can be directly an attribute of the object, or any computed value based on that object (e.g. the math test score plus the English test score of a student).

score_fn = lambda x: x["year"]

Let's run the ranking operation on this list of languages

from pyranking import rank
ranked_languages = rank(languages, score_fn);

The ranked_languages is an array that looks like below. By default, it sorts by descending order, using the competition strategy, and starts ranking at 1

[ { "rank": 1, "item": { "name": 'Go', "year": 2009 } },
  { "rank": 2, "item": { "name": 'Groovy', "year": 2003 } },
  { "rank": 2, "item": { "name": 'Scala', "year": 2003 } },
  { "rank": 4, "item": { "name": 'C#', "year": 2001 } },
  { "rank": 5, "item": { "name": 'Javascript', "year": 1995 } },
  { "rank": 5, "item": { "name": 'Java', "year": 1995 } } ]

To change the ranking strategy, use the optional parameter strategy.

const ranked_anguages = rank(languages, score_fn, strategy="dense");

To sort in ascending order

const ranked_languages = rank(languages, score_fn, reverse=True);

To start ranking at any number (e.g. 5).

const ranked_anguages = rank(languages, score_fn, start=5);
// Go language would have a rank of 5.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyranking-0.1.0.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyranking-0.1.0-py3-none-any.whl (3.2 kB view details)

Uploaded Python 3

File details

Details for the file pyranking-0.1.0.tar.gz.

File metadata

  • Download URL: pyranking-0.1.0.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for pyranking-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f1729e5d7404690baf1191f5418aabb8a9845fa33109bbe9dca418e7d1ae2263
MD5 9c98ad239e65f18ad743847fcfb7b2f6
BLAKE2b-256 609513389a9be01d45a0ebc5f9728b598709f0d696af944596fddf12e13bb073

See more details on using hashes here.

File details

Details for the file pyranking-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pyranking-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for pyranking-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8340689aa79ebe567b5f0cc775725acc08710c2cb370db73ef90e0071f7d877d
MD5 73b2abebcbf6fd92ac02701a9563b63b
BLAKE2b-256 fe199862e7586eca082680c26571b8fdf6d16fcd8beffd525078ce78825ec956

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page