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
2fb4d519
Commit
2fb4d519
authored
Oct 21, 2003
by
Andrew M. Kuchling
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Document list.sort() changes
parent
57172081
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
1 deletion
+37
-1
Doc/whatsnew/whatsnew24.tex
Doc/whatsnew/whatsnew24.tex
+37
-1
No files found.
Doc/whatsnew/whatsnew24.tex
View file @
2fb4d519
...
...
@@ -38,7 +38,43 @@ Here are all of the changes that Python 2.4 makes to the core Python
language.
\begin{itemize}
\item
TBD
\item
The
\method
{
sort()
}
method of lists gained three keyword
arguments,
\var
{
cmp
}
,
\var
{
key
}
, and
\var
{
reverse
}
. These arguments
make some common usages of
\method
{
sort()
}
simpler. All are optional.
\var
{
cmp
}
is the same as the previous single argument to
\method
{
sort()
}
; if provided, the value should be a comparison
function that takes two arguments and returns -1, 0, or +1 depending
on how the arguments compare.
\var
{
key
}
should be a single-argument function that takes a list
element and returns a comparison key for the element. The list is
then sorted using the comparison keys. The following example sorts a list
case-insensitively:
\begin{verbatim}
>>> L = ['A', 'b', 'c', 'D']
>>> L.sort() # Case-sensitive sort
>>> L
['A', 'D', 'b', 'c']
>>> L.sort(key=lambda x: x.lower())
>>> L
['A', 'b', 'c', 'D']
>>> L.sort(cmp=lambda x,y: cmp(x.lower(), y.lower()))
>>> L
['A', 'b', 'c', 'D']
\end{verbatim}
The last example, which uses the
\var
{
cmp
}
parameter, is the old way
to perform a case-insensitive sort. It works, but is slower than
using a
\var
{
key
}
parameter. Using
\var
{
key
}
results in calling the
\method
{
lower()
}
method once for each element in the list while using
\var
{
cmp
}
will call the method twice for each comparison.
The
\var
{
reverse
}
parameter should have a Boolean value. If the value is
\constant
{
True
}
, the list will be sorted into reverse order. Instead
of
\code
{
L.sort() ; L.reverse()
}
, you can now write
\code
{
L.sort(reverse=True)
}
.
\end{itemize}
...
...
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