*Memos:
- My post explains itertools about count(), cycle() and repeat().
- 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 pairwise(), starmap(), tee() and zip_longest().
- My post explains itertools about product() and permutations().
- My post explains itertools about combinations() and combinations_with_replacement().
itertools has the functions to create iterators.
*more-itertools has more functions by installing with pip install more-itertools
.
groupby() can return the iterator which groups the elements of iterable
by key
one by one to return a set of the key and element one by one as shown below:
*Memos:
- The 1st argument is
iterable
(Required-Type:iterable
). - The 2nd argument is
key
(Optional-Default:None
-Type:callable
).
from itertools import groupby v = groupby(iterable=['AB', 'CDE', 'FG', 'H', 'IJK']) v = groupby(iterable=['AB', 'CDE', 'FG', 'H', 'IJK'], key=None) print(v) # <itertools.groupby object at 0x0000026906F60B20> key, x = next(v) print(key, next(x)) # AB AB key, x = next(v) print(key, next(x)) # CDE CDE key, x = next(v) print(key, next(x)) # FG FG key, x = next(v) print(key, next(x)) # H H key, x = next(v) print(key, next(x)) # IJK IJK key, x = next(v) # StopIteration:
from itertools import groupby for key, x in groupby(iterable=['AB', 'CDE', 'FG', 'H', 'IJK']): print(key, next(x)) # AB AB # CDE CDE # FG FG # H H # IJK IJK
from itertools import groupby for key, x in groupby(iterable=['AB', 'CDE', 'FG', 'H', 'IJK'], key=len): print(key, next(x)) # 2 AB # 3 CDE # 2 FG # 1 H # 3 IJK
from itertools import groupby d = {} for key, x in groupby(iterable=['AB', 'CDE', 'FG', 'H', 'IJK'], key=len): if not key in d: d.update({key:[]}) d[key].append(next(x)) print(d) # {2: ['AB', 'FG'], 3: ['CDE', 'IJK'], 1: ['H']}
islice() can return the iterator which returns the selected elements of iterable
one by one as shown below:
*Memos:
- The 1st argument is
iterable
(Required-Type:iterable
). - The 2nd argument is
start
(Required-Type:int
): *Memos:- It must be
0 <= x
. - If it's
None
or not set, it's0
.
- It must be
- The 2nd(Required) or 3rd(Optional) argument is
stop
(Type:int
): *Memos:- It must be
0 <= x
. - If it's
None
or not set,iterable
can be read to the end.
- It must be
- The 4th argument is
step
(Optional-Type:int
): *Memos:- It must be
1 <= x
. - If it's
None
or not set, it's1
.
- It must be
- Only if two arguments are set, the 2nd argument is
stop
.
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 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
Top comments (0)