@@ -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
@@ -851,27 +851,20 @@ which incur interpreter overhead.
851
851
"Returns the nth item or a default value."
852
852
return next(islice(iterable, n, None), default)
853
853
854
- def quantify(iterable, pred =bool):
854
+ def quantify(iterable, predicate =bool):
855
855
"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)
857
863
858
864
def all_equal(iterable, key=None):
859
865
"Returns True if all the elements are equal to each other."
860
866
return len(take(2, groupby(iterable, key))) <= 1
861
867
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
-
875
868
def unique_everseen(iterable, key=None):
876
869
"List unique elements, preserving order. Remember all elements ever seen."
877
870
# unique_everseen('AAAABBBCCDAABBB') --> A B C D
@@ -956,14 +949,14 @@ which incur interpreter overhead.
956
949
num_active -= 1
957
950
nexts = cycle(islice(nexts, num_active))
958
951
959
- def partition(pred , iterable):
952
+ def partition(predicate , iterable):
960
953
"""Partition entries into false entries and true entries.
961
954
962
- If *pred * is slow, consider wrapping it with functools.lru_cache().
955
+ If *predicate * is slow, consider wrapping it with functools.lru_cache().
963
956
"""
964
957
# partition(is_odd, range(10)) --> 0 2 4 6 8 and 1 3 5 7 9
965
958
t1, t2 = tee(iterable)
966
- return filterfalse(pred , t1), filter(pred , t2)
959
+ return filterfalse(predicate , t1), filter(predicate , t2)
967
960
968
961
def subslices(seq):
969
962
"Return all contiguous non-empty subslices of a sequence."
@@ -1206,7 +1199,7 @@ The following recipes have a more mathematical flavor:
1206
1199
>>> quantify([True , False , False , True , True ])
1207
1200
3
1208
1201
1209
- >>> quantify(range (12 ), pred = lambda x : x% 2 == 1 )
1202
+ >>> quantify(range (12 ), predicate = lambda x : x% 2 == 1 )
1210
1203
6
1211
1204
1212
1205
>>> a = [[1 , 2 , 3 ], [4 , 5 , 6 ]]
0 commit comments