@@ -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
@@ -859,27 +859,20 @@ which incur interpreter overhead.
859859 "Returns the nth item or a default value."
860860 return next(islice(iterable, n, None), default)
861861
862- def quantify(iterable, pred =bool):
862+ def quantify(iterable, predicate =bool):
863863 "Given a predicate that returns True or False, count the True results."
864- return sum(map(pred, iterable))
864+ return sum(map(predicate, iterable))
865+
866+ def first_true(iterable, default=False, predicate=None):
867+ "Returns the first true value or the *default * if there is no true value."
868+ # first_true([a,b,c], x) --> a or b or c or x
869+ # first_true([a,b], x, f) --> a if f(a) else b if f(b) else x
870+ return next(filter(predicate, iterable), default)
865871
866872 def all_equal(iterable, key=None):
867873 "Returns True if all the elements are equal to each other."
868874 return len(take(2, groupby(iterable, key))) <= 1
869875
870- def first_true(iterable, default=False, pred=None):
871- """Returns the first true value in the iterable.
872-
873- If no true value is found, returns *default *
874-
875- If *pred * is not None, returns the first item
876- for which pred(item) is true.
877-
878- """
879- # first_true([a,b,c], x) --> a or b or c or x
880- # first_true([a,b], x, f) --> a if f(a) else b if f(b) else x
881- return next(filter(pred, iterable), default)
882-
883876 def unique_everseen(iterable, key=None):
884877 "List unique elements, preserving order. Remember all elements ever seen."
885878 # unique_everseen('AAAABBBCCDAABBB') --> A B C D
@@ -964,14 +957,14 @@ which incur interpreter overhead.
964957 num_active -= 1
965958 nexts = cycle(islice(nexts, num_active))
966959
967- def partition(pred , iterable):
960+ def partition(predicate , iterable):
968961 """Partition entries into false entries and true entries.
969962
970- If *pred * is slow, consider wrapping it with functools.lru_cache().
963+ If *predicate * is slow, consider wrapping it with functools.lru_cache().
971964 """
972965 # partition(is_odd, range(10)) --> 0 2 4 6 8 and 1 3 5 7 9
973966 t1, t2 = tee(iterable)
974- return filterfalse(pred , t1), filter(pred , t2)
967+ return filterfalse(predicate , t1), filter(predicate , t2)
975968
976969 def subslices(seq):
977970 "Return all contiguous non-empty subslices of a sequence."
@@ -1214,7 +1207,7 @@ The following recipes have a more mathematical flavor:
12141207 >>> quantify([True , False , False , True , True ])
12151208 3
12161209
1217- >>> quantify(range (12 ), pred = lambda x : x% 2 == 1 )
1210+ >>> quantify(range (12 ), predicate = lambda x : x% 2 == 1 )
12181211 6
12191212
12201213 >>> a = [[1 , 2 , 3 ], [4 , 5 , 6 ]]
0 commit comments