*Memo for itertools:
- My post explains cycle() and repeat().
- My post explains accumulate() and batched().
- My post explains chain(), chain.from_iterable() and compress().
- My post explains filterfalse(), takewhile() and dropwhile().
- My post explains groupby().
- My post explains islice() and pairwise().
- My post explains starmap(), tee() and zip_longest().
- My post explains product().
- My post explains permutations().
- My post explains combinations() and combinations_with_replacement().
*Memo for others:
- 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:
*Memo:
- The 1st argument is
start(Optional-Default:0-Type:int/float/complex):- It's a start number.
- The 2nd argument is
step(Optional-Default:1-Type:int/float/complex):- It's the interval of numbers.
from itertools import count
v = count()
v = count(start=0, step=1)
print(v)
# count(0)
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(v) # count(3)
print(v) # count(3)
print(next(v)) # 3
print(next(v)) # 4
print(v) # count(5)
print(v) # count(5)
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
Top comments (0)