@@ -56,13 +56,13 @@ Iterator Arguments Results
56
56
:func: `chain ` p, q, ... p0, p1, ... plast, q0, q1, ... ``chain('ABC', 'DEF') --> A B C D E F ``
57
57
:func: `chain.from_iterable ` iterable p0, p1, ... plast, q0, q1, ... ``chain.from_iterable(['ABC', 'DEF']) --> A B C D E F ``
58
58
: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 ``
61
61
:func: `groupby ` iterable[, key] sub-iterators grouped by value of key(v)
62
62
:func: `islice ` seq, [start,] stop [, step] elements from seq[start:stop:step] ``islice('ABCDEFG', 2, None) --> C D E F G ``
63
63
:func: `pairwise ` iterable (p[0], p[1]), (p[1], p[2]) ``pairwise('ABCDEFG') --> AB BC CD DE EF FG ``
64
64
: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 ``
66
66
:func: `tee ` it, n it1, it2, ... itn splits one iterator into n
67
67
:func: `zip_longest ` p, q, ... (p[0], q[0]), (p[1], q[1]), ... ``zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D- ``
68
68
============================ ============================ ================================================= =============================================================
@@ -90,7 +90,7 @@ Examples Results
90
90
91
91
.. _itertools-functions :
92
92
93
- Itertool functions
93
+ Itertool Functions
94
94
------------------
95
95
96
96
The following module functions all construct and return iterators. Some provide
@@ -859,27 +859,20 @@ which incur interpreter overhead.
859
859
"Returns the nth item or a default value."
860
860
return next(islice(iterable, n, None), default)
861
861
862
- def quantify(iterable, pred =bool):
862
+ def quantify(iterable, predicate =bool):
863
863
"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)
865
871
866
872
def all_equal(iterable, key=None):
867
873
"Returns True if all the elements are equal to each other."
868
874
return len(take(2, groupby(iterable, key))) <= 1
869
875
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
-
883
876
def unique_everseen(iterable, key=None):
884
877
"List unique elements, preserving order. Remember all elements ever seen."
885
878
# unique_everseen('AAAABBBCCDAABBB') --> A B C D
@@ -964,14 +957,14 @@ which incur interpreter overhead.
964
957
num_active -= 1
965
958
nexts = cycle(islice(nexts, num_active))
966
959
967
- def partition(pred , iterable):
960
+ def partition(predicate , iterable):
968
961
"""Partition entries into false entries and true entries.
969
962
970
- If *pred * is slow, consider wrapping it with functools.lru_cache().
963
+ If *predicate * is slow, consider wrapping it with functools.lru_cache().
971
964
"""
972
965
# partition(is_odd, range(10)) --> 0 2 4 6 8 and 1 3 5 7 9
973
966
t1, t2 = tee(iterable)
974
- return filterfalse(pred , t1), filter(pred , t2)
967
+ return filterfalse(predicate , t1), filter(predicate , t2)
975
968
976
969
def subslices(seq):
977
970
"Return all contiguous non-empty subslices of a sequence."
@@ -1214,7 +1207,7 @@ The following recipes have a more mathematical flavor:
1214
1207
>>> quantify([True , False , False , True , True ])
1215
1208
3
1216
1209
1217
- >>> quantify(range (12 ), pred = lambda x : x% 2 == 1 )
1210
+ >>> quantify(range (12 ), predicate = lambda x : x% 2 == 1 )
1218
1211
6
1219
1212
1220
1213
>>> a = [[1 , 2 , 3 ], [4 , 5 , 6 ]]
0 commit comments