Skip to content
EntityQ7433174· pop 6· linked from 4 articles

Schwartzian transform

Sign in to save

Also known as decorate-sort-undecorate

programming idiom for efficiently sorting a list by a computed key

Described at

Sorting HOW TO — Python 3.11.15 documentation

Author, Andrew Dalke and Raymond Hettinger,, Release, 0.1,. Python lists have a built-in list.sort() method that modifies the list in-place. There is also a sorted() built-in function that builds a...

docs.python.org

Python lists have a built-in list.sort() method that modifies the list in-place. There is also a sorted() built-in function that builds a new sorted list from an iterable. In this document, we explore the various techniques for sorting data using Python. A simple ascending sort is very easy: just call the sorted() function. It returns a new sorted list: You can also use the list.sort() method. It modifies the list in-place (and returns None to avoid confusion). Usually it’s less convenient than sorted() but if you don’t need the original list, it’s slightly more efficient. Another difference is that the list.sort() method is only defined for lists. In contrast, the sorted() function accepts any iterable. The value of the key parameter should be a function (or other callable) that takes a single argument and returns a key to use for sorting purposes. This technique is fast because the key function is called exactly once for each input record. The key-function patterns shown above are very common, so Python provides convenience functions to make accessor functions easier and faster. The operator module has itemgetter() , attrgetter() , and a methodcaller() function. Both list.sort() and sorted() accept a reverse parameter with a boolean value. This is used to flag descending sorts. For example, to get the student data in reverse age order: Sorts are guaranteed to be stable . That means that when multiple records have the same key, their original order is preserved. This wonderful property lets you build complex sorts in a series of sorting steps. For example, to sort the student data by descending grade and then ascending age , do the age sort first and then sort again using grade : This can be abstracted out into a wrapper function that can take a list and tuples of field and order to sort them on multiple passes. The Timsort algorithm used in Python does multiple sorts efficiently because it can take advantage of any ordering already present in a dataset. First, the initial list is decorated with new values that control the sort order. Finally, the decorations are removed, creating a list that contains only the initial values in the new order. This idiom works because tuples are compared lexicographically; the first items are compared; if they are the same then the second items are compared, and so on. The sort is stable – if two items have the same key, their order will be preserved in the sorted list. The original items do not have to be comparable because the ordering of the decorated tuples will be determined by at most the first two items. So for example the original list could contain complex numbers which cannot be sorted directly. Another name for this idiom is Schwartzian transform , after Randal L. Schwartz, who popularized it among Perl programmers. Now that Python sorting provides key-functions, this technique is not often needed. Unlike key functions that return an absolute value for sorting, a comparison function computes the relative ordering for two inputs. For example, a balance scale compares two samples giving a relative ordering: lighter, equal, or heavier. Likewise, a comparison function such as cmp(a, b) will return a negative value for less-than, zero if the inputs are equal, or a positive value for greater-than. It is common to encounter comparison functions when translating algorithms from other languages. Also, some libraries provide comparison functions as part of their API. For example, locale.strcoll() is a comparison function. For locale aware sorting, use locale.strxfrm() for a key function or locale.strcoll() for a comparison function. This is necessary because “alphabetical” sort orderings can vary across cultures even if the underlying alphabet is the same. The reverse parameter still maintains sort stability (so that records with equal keys retain the original order). Interestingly, that effect can be simulated without the parameter by using the builtin reversed() funct

Excerpt from a page describing this subject · 17,675 chars · not written by Vinony

Wikidata facts

Show 1 more fact

via Wikidata · CC0

Available in 5 languages

via Wikidata sitelinks · CC0

Connections

Categories