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
d379d637
Commit
d379d637
authored
Aug 09, 2016
by
Zachary Ware
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #27204: Fix doctests in Doc/howto
Initial patch by Jelle Zijlstra.
parent
fd16fcaf
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
18 deletions
+25
-18
Doc/howto/sorting.rst
Doc/howto/sorting.rst
+25
-18
No files found.
Doc/howto/sorting.rst
View file @
d379d637
...
@@ -59,28 +59,28 @@ A common pattern is to sort complex objects using some of the object's indices
...
@@ -59,28 +59,28 @@ A common pattern is to sort complex objects using some of the object's indices
as keys. For example:
as keys. For example:
>>> student_tuples = [
>>> student_tuples = [
('john', 'A', 15),
...
('john', 'A', 15),
('jane', 'B', 12),
...
('jane', 'B', 12),
('dave', 'B', 10),
...
('dave', 'B', 10),
]
...
]
>>> sorted(student_tuples, key=lambda student: student[2]) # sort by age
>>> sorted(student_tuples, key=lambda student: student[2]) # sort by age
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
The same technique works for objects with named attributes. For example:
The same technique works for objects with named attributes. For example:
>>> class Student:
>>> class Student:
def __init__(self, name, grade, age):
...
def __init__(self, name, grade, age):
self.name = name
...
self.name = name
self.grade = grade
...
self.grade = grade
self.age = age
...
self.age = age
def __repr__(self):
...
def __repr__(self):
return repr((self.name, self.grade, self.age))
...
return repr((self.name, self.grade, self.age))
>>> student_objects = [
>>> student_objects = [
Student('john', 'A', 15),
...
Student('john', 'A', 15),
Student('jane', 'B', 12),
...
Student('jane', 'B', 12),
Student('dave', 'B', 10),
...
Student('dave', 'B', 10),
]
...
]
>>> sorted(student_objects, key=lambda student: student.age) # sort by age
>>> sorted(student_objects, key=lambda student: student.age) # sort by age
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
...
@@ -116,6 +116,7 @@ parameters for each object being sorted. For example, the :meth:`str.count`
...
@@ -116,6 +116,7 @@ parameters for each object being sorted. For example, the :meth:`str.count`
method could be used to compute message priority by counting the
method could be used to compute message priority by counting the
number of exclamation marks in a message:
number of exclamation marks in a message:
>>> from operator import methodcaller
>>> messages = ['critical!!!', 'hurry!', 'standby', 'immediate!!']
>>> messages = ['critical!!!', 'hurry!', 'standby', 'immediate!!']
>>> sorted(messages, key=methodcaller('count', '!'))
>>> sorted(messages, key=methodcaller('count', '!'))
['standby', 'hurry!', 'immediate!!', 'critical!!!']
['standby', 'hurry!', 'immediate!!', 'critical!!!']
...
@@ -220,15 +221,15 @@ return a negative value for less-than, return zero if they are equal, or return
...
@@ -220,15 +221,15 @@ return a negative value for less-than, return zero if they are equal, or return
a positive value for greater-than. For example, we can do:
a positive value for greater-than. For example, we can do:
>>> def numeric_compare(x, y):
>>> def numeric_compare(x, y):
return x - y
...
return x - y
>>> sorted([5, 2, 4, 1, 3], cmp=numeric_compare)
>>> sorted([5, 2, 4, 1, 3], cmp=numeric_compare)
# doctest: +SKIP
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
Or you can reverse the order of comparison with:
Or you can reverse the order of comparison with:
>>> def reverse_numeric(x, y):
>>> def reverse_numeric(x, y):
return y - x
...
return y - x
>>> sorted([5, 2, 4, 1, 3], cmp=reverse_numeric)
>>> sorted([5, 2, 4, 1, 3], cmp=reverse_numeric)
# doctest: +SKIP
[5, 4, 3, 2, 1]
[5, 4, 3, 2, 1]
When porting code from Python 2.x to 3.x, the situation can arise when you have
When porting code from Python 2.x to 3.x, the situation can arise when you have
...
@@ -256,6 +257,12 @@ function. The following wrapper makes that easy to do::
...
@@ -256,6 +257,12 @@ function. The following wrapper makes that easy to do::
To convert to a key function, just wrap the old comparison function:
To convert to a key function, just wrap the old comparison function:
.. testsetup::
from functools import cmp_to_key
.. doctest::
>>> sorted([5, 2, 4, 1, 3], key=cmp_to_key(reverse_numeric))
>>> sorted([5, 2, 4, 1, 3], key=cmp_to_key(reverse_numeric))
[5, 4, 3, 2, 1]
[5, 4, 3, 2, 1]
...
...
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