[Python-checkins] cpython (merge 3.5 -> default): Merge
raymond.hettinger
python-checkins at python.org
Sun Mar 6 21:12:14 EST 2016
https://hg.python.org/cpython/rev/e0df94327586
changeset: 100438:e0df94327586
parent: 100435:eb120f50df4a
parent: 100437:ab3e4be4b539
user: Raymond Hettinger <python at rcn.com>
date: Sun Mar 06 18:12:08 2016 -0800
summary:
Merge
files:
Doc/library/itertools.rst | 5 +++++
Lib/test/test_itertools.py | 8 ++++++++
2 files changed, 13 insertions(+), 0 deletions(-)
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst
--- a/Doc/library/itertools.rst
+++ b/Doc/library/itertools.rst
@@ -693,6 +693,11 @@
"Returns the nth item or a default value"
return next(islice(iterable, n, None), default)
+ def all_equal(iterable):
+ "Returns True if all the elements are equal to each other"
+ g = groupby(iterable)
+ return next(g, True) and not next(g, False)
+
def quantify(iterable, pred=bool):
"Count how many times the predicate is true"
return sum(map(pred, iterable))
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py
--- a/Lib/test/test_itertools.py
+++ b/Lib/test/test_itertools.py
@@ -2064,6 +2064,11 @@
... "Returns the nth item or a default value"
... return next(islice(iterable, n, None), default)
+>>> def all_equal(iterable):
+... "Returns True if all the elements are equal to each other"
+... g = groupby(iterable)
+... return next(g, True) and not next(g, False)
+
>>> def quantify(iterable, pred=bool):
... "Count how many times the predicate is true"
... return sum(map(pred, iterable))
@@ -2177,6 +2182,9 @@
>>> nth('abcde', 9) is None
True
+>>> [all_equal(s) for s in ('', 'A', 'AAAA', 'AAAB', 'AAABA')]
+[True, True, True, False, False]
+
>>> quantify(range(99), lambda x: x%2==0)
50
--
Repository URL: https://hg.python.org/cpython
More information about the Python-checkins
mailing list