[Python-checkins] cpython (3.5): Improve recipe by showing results of intermediate steps

raymond.hettinger python-checkins at python.org
Mon Sep 5 16:16:17 EDT 2016


https://hg.python.org/cpython/rev/096b712cc129
changeset: 103058:096b712cc129
branch: 3.5
parent: 103049:e0873191ad7d
user: Raymond Hettinger <python at rcn.com>
date: Mon Sep 05 13:15:02 2016 -0700
summary:
 Improve recipe by showing results of intermediate steps
files:
 Doc/library/random.rst | 6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
diff --git a/Doc/library/random.rst b/Doc/library/random.rst
--- a/Doc/library/random.rst
+++ b/Doc/library/random.rst
@@ -328,6 +328,9 @@
 
 >>> weighted_choices = [('Red', 3), ('Blue', 2), ('Yellow', 1), ('Green', 4)]
 >>> population = [val for val, cnt in weighted_choices for i in range(cnt)]
+ >>> population
+ ['Red', 'Red', 'Red', 'Blue', 'Blue', 'Yellow', 'Green', 'Green', 'Green', 'Green']
+
 >>> random.choice(population)
 'Green'
 
@@ -337,6 +340,9 @@
 
 >>> choices, weights = zip(*weighted_choices)
 >>> cumdist = list(itertools.accumulate(weights))
+ >>> cumdist # [3, 3+2, 3+2+1, 3+2+1+4]
+ [3, 5, 6, 10]
+
 >>> x = random.random() * cumdist[-1]
 >>> choices[bisect.bisect(cumdist, x)]
 'Blue'
-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /