Skip to main content

Sort a sequence, preferring some values

Project description

https://img.shields.io/pypi/v/prefsort.svg https://img.shields.io/travis/jonathaneunice/prefsort.svg Documentation Status Updates

Partially sort a sequence, preferring some values.

from prefsort import prefsorted

seq = list('abcde')

seq2 = prefsorted(seq, 'c b')
assert seq2 == ['c', 'b', 'a', 'd', 'e']

Note that this doesn’t sort the majority of the sequence in the way Python’s normal list.sort() or sorted() do. It just pulls the preferred members to the front of the list.

This is particularly handy to have when organizing data columns, for example with pandas, the following will make sure the id and name columns come first in a DataFrame:

df = df.reindex(columns=prefsorted(df.columns, 'id name'))

There is also a reverse parameter that will put the “preferred” items at the end of the list. In this case it’s a “negative preference.”

seq2 = prefsorted(seq, 'c b', reverse=True)
assert seq2 == ['a', 'd', 'e', 'c', 'b']

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

prefsort-0.1.0.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

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

prefsort-0.1.0-py2.py3-none-any.whl (3.4 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: prefsort-0.1.0.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

File hashes

Hashes for prefsort-0.1.0.tar.gz
Algorithm Hash digest
SHA256 22c1359cb59bde9c0af494fcd0ac241fd62302b5dfb9f875837ec41f0d2ddf3b
MD5 d5c48727a144485dc83b2b75f7cb3457
BLAKE2b-256 e98a1b7680bb97af5088480a5d539834e7af40553e604f6947310ce870c5bc59

See more details on using hashes here.

File details

Details for the file prefsort-0.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: prefsort-0.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 3.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

File hashes

Hashes for prefsort-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b27af30c1ee3ad86402cfffd0721872459d9eb53c06972c9de6fd0b7d4837edb
MD5 68ce8ec5fa32119252d7f0714c24aa62
BLAKE2b-256 b7ddc214416c70d68ccebbdf2e7392bd29f3a0c8cb8ffa3726f12ba2448bdb9f

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