mirror of
https://github.com/python/cpython.git
synced 2025-10-08 08:01:55 +00:00
Document another recipe for itertools: all_equal(). Inspired by David Beazley.
This commit is contained in:
parent
d66dd5ce68
commit
e525ee3b48
2 changed files with 13 additions and 0 deletions
|
@ -2014,6 +2014,11 @@ Samuele
|
|||
... "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))
|
||||
|
@ -2127,6 +2132,9 @@ perform as purported.
|
|||
>>> 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
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue