Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
047ada4e
Commit
047ada4e
authored
May 26, 2016
by
Benjamin Peterson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
remove cruft from Schwarzian transform section
parent
6d3ad2f6
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
1 addition
and
30 deletions
+1
-30
Doc/faq/programming.rst
Doc/faq/programming.rst
+1
-30
No files found.
Doc/faq/programming.rst
View file @
047ada4e
...
@@ -1312,40 +1312,11 @@ I want to do a complicated sort: can you do a Schwartzian Transform in Python?
...
@@ -1312,40 +1312,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
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
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 = L[:]
Isorted.sort(key=lambda s: int(s[10:15]))
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]
For versions prior to 3.0, 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?
How can I sort one list by values from another list?
----------------------------------------------------
----------------------------------------------------
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment