Improve three recipes in the itertools docs.

This commit is contained in:
Raymond Hettinger 2004-09-23 07:27:39 +00:00
parent 513c8bd6f2
commit 4533f1fb7f
2 changed files with 19 additions and 7 deletions

View File

@ -458,15 +458,21 @@ def nth(iterable, n):
def all(seq, pred=bool):
"Returns True if pred(x) is True for every element in the iterable"
return False not in imap(pred, seq)
for elem in ifilterfalse(pred, seq):
return False
return True
def any(seq, pred=bool):
"Returns True if pred(x) is True at least one element in the iterable"
return True in imap(pred, seq)
"Returns True if pred(x) is True for at least one element in the iterable"
for elem in ifilter(pred, seq):
return True
return False
def no(seq, pred=bool):
"Returns True if pred(x) is False for every element in the iterable"
return True not in imap(pred, seq)
for elem in ifilter(pred, seq):
return False
return True
def quantify(seq, pred=bool):
"Count how many times the predicate is True in the sequence"

View File

@ -746,15 +746,21 @@ Samuele
>>> def all(seq, pred=bool):
... "Returns True if pred(x) is True for every element in the iterable"
... return False not in imap(pred, seq)
... for elem in ifilterfalse(pred, seq):
... return False
... return True
>>> def any(seq, pred=bool):
... "Returns True if pred(x) is True for at least one element in the iterable"
... return True in imap(pred, seq)
... for elem in ifilter(pred, seq):
... return True
... return False
>>> def no(seq, pred=bool):
... "Returns True if pred(x) is False for every element in the iterable"
... return True not in imap(pred, seq)
... for elem in ifilter(pred, seq):
... return False
... return True
>>> def quantify(seq, pred=bool):
... "Count how many times the predicate is True in the sequence"