*Memos:
- My post explains itertools about accumulate(), batched(), chain() and chain.from_iterable().
- My post explains itertools about compress(), filterfalse(), takewhile() and dropwhile().
- My post explains itertools about groupby() and islice().
- My post explains itertools about pairwise(), starmap(), tee() and zip_longest().
- My post explains itertools about product() and permutations().
- My post explains itertools about combinations() and combinations_with_replacement().
- My post explains an iterator (1).
itertools has the functions to create iterators.
*more-itertools has more functions by installing with pip install more-itertools
.
count() can return the iterator which endlessly generates a number one by one as shown below:
*Memos:
- The 1st argument is
start
(Optional-Default:0
-Type:int
,float
,complex
orbool
). - The 2nd argument is
step
(Optional-Default:1
-Type:int
,float
,complex
orbool
).
from itertools import count v = count() v = count(start=0, step=1) print(type(v)) # <class 'itertools.count'> print(next(v)) # 0 print(next(v)) # 1 print(next(v)) # 2 print(next(v)) # 3 print(next(v)) # 4
from itertools import count v = count() print(v) # count(0) print(v) # count(0) print(next(v)) # 0 print(v) # count(1) print(v) # count(1) print(next(v)) # 1 print(next(v)) # 2 print(next(v)) # 3 print(next(v)) # 4
from itertools import count for x in count(): if x == 5: break print(x) # 0 # 1 # 2 # 3 # 4
from itertools import count for x in count(start=-5, step=3): if x == 10: break print(x) # -5 # -2 # 1 # 4 # 7
from itertools import count for x in count(start=-5.0, step=3.0): if x == 10: break print(x) # -5.0 # -2.0 # 1.0 # 4.0 # 7.0
from itertools import count for x in count(start=-5.0+0.0j, step=3.0+0.0j): if x == 10: break print(x) # (-5+0j) # (-2+0j) # (1+0j) # (4+0j) # (7+0j)
from itertools import count for x in count(start=5, step=-3): if x == -10: break print(x) # 5 # 2 # -1 # -4 # -7
cycle() can return the iterator which endlessly repeats the elements of iterable
one by one as shown below:
*Memos:
- The 1st argument is
iterable
(Required-Type:iterable
). - Don't use
iterable=
.
from itertools import cycle v = cycle('ABC') v = cycle(['A', 'B', 'C']) print(v) # <itertools.cycle object at 0x0000026906F4EA00> print(next(v)) # A print(next(v)) # B print(next(v)) # C print(next(v)) # A print(next(v)) # B print(next(v)) # C print(next(v)) # A print(next(v)) # B
from itertools import cycle count = 0 for x in cycle('ABC'): if count == 8: break print(x) count += 1 # A # B # C # A # B # C # A # B
repeat() can return the iterator which endlessly or limitedly repeats object
as shown below:
*Memos:
- The 1st argument is
object
(Required-Type:object
). - The 2nd argument is
times
(Optional-Type:int
). *If it's set,object
is limitedly repeated otherwiseobject
is endlessly repeated.
from itertools import repeat v = repeat(object='Hello') print(v) # repeat('Hello') print(type(v)) # <class 'itertools.repeat'> print(next(v)) # Hello print(next(v)) # Hello print(next(v)) # Hello print(next(v)) # Hello print(next(v)) # Hello
from itertools import repeat v = repeat(object='Hello', times=3) print(next(v)) # Hello print(next(v)) # Hello print(next(v)) # Hello print(next(v)) # StopIteration:
from itertools import repeat for x in repeat(object='Hello', times=3): print(x) # Hello # Hello # Hello
Top comments (0)