Related Documents
Documents Link
pymotw link
Unlimited iterator
itertools.count()
Explanation
Generate an infinite number of iterations queue, only be
parameter
In fact, numbers, and stride
start, [step]
return value
start, start+step, start+2*step, ...
Examples
count(10) --> 10 11 12 13 14 ...
from itertools import * import time c = count(10,5) print c for i in c: time.sleep(0.5) print i """ count(10, 5) 10 15 20 25 30 35 40 45 50 55 Process finished with exit code -1 """
itertools.cycle()
Explanation
Iterable then passed in a loop through this object
parameter
p
return value
p0, p1, ... plastic, p0, p1, ...
Examples
cycle('ABCD') --> A B C D A B C D ...
from itertools import * import time c = cycle([1, 2, 3]) print c for i in c: time.sleep(0.5) print i """ <itertools.cycle object at 0x0000000002A42088> 1 2 3 1 2 3 1 2 Process finished with exit code -1 """
itertools.repeat()
Explanation
Passing an object, as well as digital, digital traversal of this object specifies the number of times
parameter
element [n]
return value
elem, elem, elem, ... endlessly or up to n times
Examples
repeat(10, 3) --> 10 10 10
from itertools import * import time c = repeat(3, 5) print c for i in c: time.sleep(0.5) print i """ repeat(3, 5) 3 3 3 3 3 Process finished with exit code 0 """
Processing the input sequence iterator
itertools.chain()
Explanation
Passing a plurality of sequences, when traversing a plurality of sequences performed together in series
parameter
p, q, ...
return value
p0, p1, ... plastic, q0, q1, ...
Examples
chain('ABC', 'DEF') --> A B C D E F
from itertools import * c = chain(range(5), range(3)) print c for i in c: print i """ <itertools.chain object at 0x0000000002A77FD0> 0 1 2 3 4 0 1 2 Process finished with exit code 0r """
itertools.compress()
Explanation
Parameters passed in a sequence and a select list of data sequences were screened based on the selection list
parameter
data, selectors
return value
(d[0] if s[0]), (d[1] if s[1]), ...
Examples
compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F
from itertools import * c = compress("hahahahahaha", [1, 0, 1, 0, 1, 0, 1, 0]) print c for i in c: print i """ <itertools.compress object at 0x0000000002B01048> h h h h Process finished with exit code 0 """
itertools.dropwhile()
Explanation
Create an iterator, as long as the function predicate (Item) True, the item is discarded in iterable, if the predicate returns False, iterable generates the key and all subsequent entries.
Namely: the first time after the condition is false, returns an iterator to the remaining items.
parameter
pred, seq
return value
seq[n], seq[n+1], starting when pred fails
Examples
dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1
def dropwhile(predicate, iterable): # dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1 iterable = iter(iterable) for x in iterable: if not predicate(x): yield x break for x in iterable: yield x
from itertools import * def should_drop(x): print 'Testing:', x return (x<1) for i in dropwhile(should_drop, [ -1, 0, 1, 2, 3, 4, 1, -2 ]): print 'Yielding:', i Testing: -1 Testing: 0 Testing: 1 Yielding: 1 Yielding: 2 Yielding: 3 Yielding: 4 Yielding: 1 Yielding: -2
itertools.groupby()
Explanation
Keyfunc function according to the result of the grouping execution sequence of each element (each packet is an iterator), returns an iterator these packets
parameter
iterable[, keyfunc]
return value
sub-iterators grouped by value of keyfunc(v)
Examples
chain('ABC', 'DEF') --> A B C D E F
迭代器 参数 结果 例子 ifilter() pred, seq elements of seq where pred(elem) is True ifilter(lambda x: x%2, range(10)) --> 1 3 5 7 9 ifilterfalse() pred, seq elements of seq where pred(elem) is False ifilterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8 islice() seq, [start,] stop [, step] elements from seq[start:stop:step] islice('ABCDEFG', 2, None) --> C D E F G imap() func, p, q, ... func(p0, q0), func(p1, q1), ... imap(pow, (2,3,10), (5,2,3)) --> 32 9 1000 starmap() func, seq func(*seq[0]), func(*seq[1]), ... starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000 tee() it, n it1, it2 , ... itn splits one iterator into n takewhile() pred, seq seq[0], seq[1], until pred fails takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4 izip() p, q, ... (p[0], q[0]), (p[1], q[1]), ... izip('ABCD', 'xy') --> Ax By izip_longest() p, q, ... (p[0], q[0]), (p[1], q[1]), ... izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-