diff options
author | Benjamin Peterson <benjamin@python.org> | 2016-05-27 05:55:49 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2016-05-27 05:55:49 (GMT) |
commit | 496790b5413a28eb240033f4900099fff40b01e6 (patch) | |
tree | 827e808ae0c08f427ba48f8eb152d235f5a684d1 /Doc | |
parent | 01d2580d4bc1cd8b988d050469073b338884d6bd (diff) | |
download | cpython-496790b5413a28eb240033f4900099fff40b01e6.zip cpython-496790b5413a28eb240033f4900099fff40b01e6.tar.gz cpython-496790b5413a28eb240033f4900099fff40b01e6.tar.bz2 |
remove cruft from Schwarzian transform section
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/faq/programming.rst | 31 |
1 files changed, 1 insertions, 30 deletions
diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst index c8190e4..9190e1a 100644 --- a/Doc/faq/programming.rst +++ b/Doc/faq/programming.rst @@ -1460,40 +1460,11 @@ I want to do a complicated sort: can you do a Schwartzian Transform in Python? The technique, attributed to Randal Schwartz of the Perl community, sorts the elements of a list by a metric which maps each element to its "sort value". In -Python, just use the ``key`` argument for the ``sort()`` method:: +Python, use the ``key`` argument for the :func:`sort()` function:: Isorted = L[:] Isorted.sort(key=lambda s: int(s[10:15])) -The ``key`` argument is new in Python 2.4, for older versions this kind of -sorting is quite simple to do with list comprehensions. To sort a list of -strings by their uppercase values:: - - tmp1 = [(x.upper(), x) for x in L] # Schwartzian transform - tmp1.sort() - Usorted = [x[1] for x in tmp1] - -To sort by the integer value of a subfield extending from positions 10-15 in -each string:: - - tmp2 = [(int(s[10:15]), s) for s in L] # Schwartzian transform - tmp2.sort() - Isorted = [x[1] for x in tmp2] - -Note that Isorted may also be computed by :: - - def intfield(s): - return int(s[10:15]) - - def Icmp(s1, s2): - return cmp(intfield(s1), intfield(s2)) - - Isorted = L[:] - Isorted.sort(Icmp) - -but since this method calls ``intfield()`` many times for each element of L, it -is slower than the Schwartzian Transform. - How can I sort one list by values from another list? ---------------------------------------------------- |