@@ -56,13 +56,13 @@ Iterator Arguments Results
5656:func: `chain ` p, q, ... p0, p1, ... plast, q0, q1, ... ``chain('ABC', 'DEF') --> A B C D E F ``
5757:func: `chain.from_iterable ` iterable p0, p1, ... plast, q0, q1, ... ``chain.from_iterable(['ABC', 'DEF']) --> A B C D E F ``
5858:func: `compress ` data, selectors (d[0] if s[0]), (d[1] if s[1]), ... ``compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F ``
59- :func: `dropwhile ` pred , seq seq[n], seq[n+1], starting when pred fails ``dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1 ``
60- :func: `filterfalse ` pred , seq elements of seq where pred (elem) is false ``filterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8 ``
59+ :func: `dropwhile ` predicate , seq seq[n], seq[n+1], starting when predicate fails ``dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1 ``
60+ :func: `filterfalse ` predicate , seq elements of seq where predicate (elem) fails ``filterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8 ``
6161:func: `groupby ` iterable[, key] sub-iterators grouped by value of key(v)
6262:func: `islice ` seq, [start,] stop [, step] elements from seq[start:stop:step] ``islice('ABCDEFG', 2, None) --> C D E F G ``
6363:func: `pairwise ` iterable (p[0], p[1]), (p[1], p[2]) ``pairwise('ABCDEFG') --> AB BC CD DE EF FG ``
6464:func: `starmap ` func, seq func(\* seq[0]), func(\* seq[1]), ... ``starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000 ``
65- :func: `takewhile ` pred , seq seq[0], seq[1], until pred fails ``takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4 ``
65+ :func: `takewhile ` predicate , seq seq[0], seq[1], until predicate fails ``takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4 ``
6666:func: `tee ` it, n it1, it2, ... itn splits one iterator into n
6767:func: `zip_longest ` p, q, ... (p[0], q[0]), (p[1], q[1]), ... ``zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D- ``
6868============================ ============================ ================================================= =============================================================
@@ -90,7 +90,7 @@ Examples Results
9090
9191.. _itertools-functions :
9292
93- Itertool functions
93+ Itertool Functions
9494------------------
9595
9696The following module functions all construct and return iterators. Some provide
@@ -851,27 +851,20 @@ which incur interpreter overhead.
851851 "Returns the nth item or a default value."
852852 return next(islice(iterable, n, None), default)
853853
854- def quantify(iterable, pred =bool):
854+ def quantify(iterable, predicate =bool):
855855 "Given a predicate that returns True or False, count the True results."
856- return sum(map(pred, iterable))
856+ return sum(map(predicate, iterable))
857+
858+ def first_true(iterable, default=False, predicate=None):
859+ "Returns the first true value or the *default * if there is no true value."
860+ # first_true([a,b,c], x) --> a or b or c or x
861+ # first_true([a,b], x, f) --> a if f(a) else b if f(b) else x
862+ return next(filter(predicate, iterable), default)
857863
858864 def all_equal(iterable, key=None):
859865 "Returns True if all the elements are equal to each other."
860866 return len(take(2, groupby(iterable, key))) <= 1
861867
862- def first_true(iterable, default=False, pred=None):
863- """Returns the first true value in the iterable.
864-
865- If no true value is found, returns *default *
866-
867- If *pred * is not None, returns the first item
868- for which pred(item) is true.
869-
870- """
871- # first_true([a,b,c], x) --> a or b or c or x
872- # first_true([a,b], x, f) --> a if f(a) else b if f(b) else x
873- return next(filter(pred, iterable), default)
874-
875868 def unique_everseen(iterable, key=None):
876869 "List unique elements, preserving order. Remember all elements ever seen."
877870 # unique_everseen('AAAABBBCCDAABBB') --> A B C D
@@ -956,14 +949,14 @@ which incur interpreter overhead.
956949 num_active -= 1
957950 nexts = cycle(islice(nexts, num_active))
958951
959- def partition(pred , iterable):
952+ def partition(predicate , iterable):
960953 """Partition entries into false entries and true entries.
961954
962- If *pred * is slow, consider wrapping it with functools.lru_cache().
955+ If *predicate * is slow, consider wrapping it with functools.lru_cache().
963956 """
964957 # partition(is_odd, range(10)) --> 0 2 4 6 8 and 1 3 5 7 9
965958 t1, t2 = tee(iterable)
966- return filterfalse(pred , t1), filter(pred , t2)
959+ return filterfalse(predicate , t1), filter(predicate , t2)
967960
968961 def subslices(seq):
969962 "Return all contiguous non-empty subslices of a sequence."
@@ -1206,7 +1199,7 @@ The following recipes have a more mathematical flavor:
12061199 >>> quantify([True , False , False , True , True ])
12071200 3
12081201
1209- >>> quantify(range (12 ), pred = lambda x : x% 2 == 1 )
1202+ >>> quantify(range (12 ), predicate = lambda x : x% 2 == 1 )
12101203 6
12111204
12121205 >>> a = [[1 , 2 , 3 ], [4 , 5 , 6 ]]
0 commit comments