*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 groupby() and islice().
- My post explains itertools about pairwise(), starmap(), tee() and zip_longest().
- My post explains itertools about product() and permutations().
itertools has the functions to create iterators.
*more-itertools has more functions by installing with pip install more-itertools
.
combinations() can return the iterator which uniquely combines the elements of iterable
one by one to return a tuple of zero or more elements one by one as shown below:
*Memos:
- The 1st argument is
iterable
(Required-Type:iterable
). - The 2nd argument is
r
(Required-Type:int
): *Memos:- It's the length of the returned tuple.
- It must be
0 <= x
.
from itertools import combinations
v = combinations(iterable='', r=0)
v = combinations(iterable=[], r=0)
print(v)
# <itertools.combinations object at 0x0000026906D95CB0>
print(next(v)) # ()
print(next(v)) # StopIteration:
from itertools import combinations
v = combinations(iterable='ABC', r=1)
v = combinations(iterable=['A', 'B', 'C'], r=1)
print(next(v)) # ('A',)
print(next(v)) # ('B',)
print(next(v)) # ('C',)
print(next(v)) # StopIteration:
from itertools import combinations
v = combinations(iterable='ABC', r=2)
print(next(v)) # ('A', 'B')
print(next(v)) # ('A', 'C')
print(next(v)) # ('B', 'C')
print(next(v)) # StopIteration:
from itertools import combinations
v = combinations(iterable='ABC', r=3)
print(next(v)) # ('A', 'B', 'C')
print(next(v)) # StopIteration:
from itertools import combinations
for x in combinations(iterable='ABC', r=3):
print(x)
# ('A', 'B', 'C')
from itertools import combinations
for x in combinations(iterable='ABC', r=4):
print(x)
# Nothing
combinations_with_replacement() can return the iterator which non-uniquely combines the elements of iterable
one by one to return a tuple of zero or more elements one by one as shown below:
*Memos:
- The 1st argument is
iterable
(Required-Type:iterable
). - The 2nd argument is
r
(Required-Type:int
): *Memos:- It's the length of the returned tuple.
- It must be
0 <= x
.
from itertools import combinations_with_replacement
v = combinations_with_replacement(iterable='', r=0)
v = combinations_with_replacement(iterable=[], r=0)
print(v)
# <itertools.combinations_with_replacement object at 0x0000026906DAAED0>
print(next(v)) # ()
print(next(v)) # StopIteration:
from itertools import combinations_with_replacement
v = combinations_with_replacement(iterable='ABC', r=1)
v = combinations_with_replacement(iterable=['A', 'B', 'C'], r=1)
print(next(v)) # ('A',)
print(next(v)) # ('B',)
print(next(v)) # ('C',)
print(next(v)) # StopIteration:
from itertools import combinations_with_replacement
v = combinations_with_replacement(iterable='ABC', r=2)
print(next(v)) # ('A', 'A')
print(next(v)) # ('A', 'B')
print(next(v)) # ('A', 'C')
print(next(v)) # ('B', 'B')
print(next(v)) # ('B', 'C')
print(next(v)) # ('C', 'C')
print(next(v)) # StopIteration:
from itertools import combinations_with_replacement
v = combinations_with_replacement(iterable='ABC', r=3)
print(next(v)) # ('A', 'A', 'A')
print(next(v)) # ('A', 'A', 'B')
print(next(v)) # ('A', 'A', 'C')
print(next(v)) # ('A', 'B', 'B')
print(next(v)) # ('A', 'B', 'C')
print(next(v)) # ('A', 'C', 'C')
print(next(v)) # ('B', 'B', 'B')
print(next(v)) # ('B', 'B', 'C')
print(next(v)) # ('B', 'C', 'C')
print(next(v)) # ('C', 'C', 'C')
print(next(v)) # StopIteration:
from itertools import combinations_with_replacement
for x in combinations_with_replacement(iterable='ABC', r=3):
print(x)
# ('A', 'A', 'A')
# ('A', 'A', 'B')
# ('A', 'A', 'C')
# ('A', 'B', 'B')
# ('A', 'B', 'C')
# ('A', 'C', 'C')
# ('B', 'B', 'B')
# ('B', 'B', 'C')
# ('B', 'C', 'C')
# ('C', 'C', 'C')
from itertools import combinations_with_replacement
for x in combinations_with_replacement(iterable='ABC', r=4):
print(x)
# ('A', 'A', 'A', 'A')
# ('A', 'A', 'A', 'B')
# ('A', 'A', 'A', 'C')
# ('A', 'A', 'B', 'B')
# ('A', 'A', 'B', 'C')
# ('A', 'A', 'C', 'C')
# ('A', 'B', 'B', 'B')
# ('A', 'B', 'B', 'C')
# ('A', 'B', 'C', 'C')
# ('A', 'C', 'C', 'C')
# ('B', 'B', 'B', 'B')
# ('B', 'B', 'B', 'C')
# ('B', 'B', 'C', 'C')
# ('B', 'C', 'C', 'C')
# ('C', 'C', 'C', 'C')
Top comments (0)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.