Skip to main content

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

Metadata

Release files for pyranking 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyranking 0.1.0
File Size Uploaded Provenance Actions
pyranking-0.1.0.tar.gz 3.2 kB Without provenance Details for pyranking-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pyranking 0.1.0
File Interpreter ABI Platform Actions
pyranking-0.1.0-py3-none-any.whl Python 3 none any Details for pyranking-0.1.0-py3-none-any.whl

Total release size: 6.4 kB

Release files / pyranking-0.1.0.tar.gz

Download URL pyranking-0.1.0.tar.gz
Size 3.2 kB
Tags Source
SHA-256 checksum
How to use checksums
f1729e5d7404690baf1191f5418aabb8a9845fa33109bbe9dca418e7d1ae2263
BLAKE2b-256 checksum
How to use checksums
609513389a9be01d45a0ebc5f9728b598709f0d696af944596fddf12e13bb073
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.12

Release files / pyranking-0.1.0-py3-none-any.whl

Download URL pyranking-0.1.0-py3-none-any.whl
Size 3.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8340689aa79ebe567b5f0cc775725acc08710c2cb370db73ef90e0071f7d877d
BLAKE2b-256 checksum
How to use checksums
fe199862e7586eca082680c26571b8fdf6d16fcd8beffd525078ce78825ec956
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.12

Security for pyranking 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page