*Memo:
islice() can return the iterator which returns the elements selected from iterable one by one in the range [start, end) as shown below:
*Memo:
- The 1st argument is
iterable(Required-Type:Iterable):- Don't use
iterable=.
- Don't use
- The 2nd argument is
startorstop(Required-Type:int/NoneType):- It's a start index(inclusive) or stop index(exclusive).
- It must be
0 <= x. - If
startisNone, it's0. - If
stopisNone,iterableis read to the end. - Don't use
start=orstop=.
- The 3rd argument is
stop(Optional-Default:None-Type:int/NoneType):- It's a stop index(exclusive).
- It must be
0 <= x. - If it's set and
None,iterableis read to the end. - If it's not set and
None, the 2nd argumentstopis prioritized. - Don't use
stop=.
- The 4th argument is
step(Optional-Default:None-Type:int/NoneType):- It's the interval of indices.
- It must be
1 <= x. - If it's
None, it's1. - Don't use
step=.
- Only if two arguments are set, the 2nd argument is
stop. -
startandstopcan only be unsigned indices(zero and positive indices). - Error doesn't occur even if
[start, end)is out of the range[The 1st element index, The last element index].
from itertools import islice
v = islice([-4, -3, -2, -1, 0, 1, 2, 3, 4], 9)
v = islice([-4, -3, -2, -1, 0, 1, 2, 3, 4], 20)
v = islice([-4, -3, -2, -1, 0, 1, 2, 3, 4], None)
v = islice([-4, -3, -2, -1, 0, 1, 2, 3, 4], 0, 9)
v = islice([-4, -3, -2, -1, 0, 1, 2, 3, 4], 0, 20)
v = islice([-4, -3, -2, -1, 0, 1, 2, 3, 4], None, None)
v = islice([-4, -3, -2, -1, 0, 1, 2, 3, 4], 0, 9, 1)
v = islice([-4, -3, -2, -1, 0, 1, 2, 3, 4], 0, 20, 1)
v = islice([-4, -3, -2, -1, 0, 1, 2, 3, 4], None, None, None)
print(v)
# <itertools.islice object at 0x0000026906E7BD30>
print(next(v)) # -4
print(next(v)) # -3
print(next(v)) # -2
print(next(v)) # -1
print(next(v)) # 0
print(next(v)) # 1
print(next(v)) # 2
print(next(v)) # 3
print(next(v)) # 4
print(next(v)) # StopIteration:
from itertools import islice
v = islice([], 0)
print(next(v))
# StopIteration:
from itertools import islice
for x in islice([-4, -3, -2, -1, 0, 1, 2, 3, 4], 4):
# for x in islice([-4, -3, -2, -1, 0, 1, 2, 3, 4], 0, 4):
# for x in islice([-4, -3, -2, -1, 0, 1, 2, 3, 4], 0, 4, 1):
print(x)
# -4
# -3
# -2
# -1
from itertools import islice
for x in islice([-4, -3, -2, -1, 0, 1, 2, 3, 4], 3, 7):
# for x in islice([-4, -3, -2, -1, 0, 1, 2, 3, 4], 3, 7, 1):
print(x)
# -1
# 0
# 1
# 2
from itertools import islice
for x in islice([-4, -3, -2, -1, 0, 1, 2, 3, 4], 1, 8, 2):
print(x)
# -3
# -1
# 1
# 3
pairwise() can return the iterator which returns a successive overlapping tuple of the two elements of iterable one by one as shown below:
*Memo:
- The 1st argument is
iterable(Required-Type:Iterable):- Don't use
iterable=.
- Don't use
from itertools import pairwise
v = pairwise('')
v = pairwise([])
print(v)
# <itertools.pairwise object at 0x0000026B3E697730>
print(next(v))
# StopIteration:
from itertools import pairwise
v = pairwise('ABCD')
v = pairwise(['A', 'B', 'C', 'D'])
print(next(v)) # ('A', 'B')
print(next(v)) # ('B', 'C')
print(next(v)) # ('C', 'D')
print(next(v)) # StopIteration:
from itertools import pairwise
for x in pairwise('ABCD'):
# for x in pairwise(['A', 'B', 'C', 'D']):
print(x)
# ('A', 'B')
# ('B', 'C')
# ('C', 'D')
Top comments (0)