CKTIS / Python
itertools.combinations_with_replacement
Kiest r posities met niet-dalende indexen en staat hergebruik van een positie toe.
Versie: Python 3.14; repository image, production runtime not attested
Syntax
itertools.combinations_with_replacement(iterable, r)Kiest r posities met niet-dalende indexen en staat hergebruik van een positie toe.
AB geeft AA, AB en BB, terwijl BA de volgorde zou omkeren.
Voor r=0 bestaat zelfs uit lege invoer één keuze zonder elementen, de lege tuple.
import itertools as I
assert list(I.combinations_with_replacement("AB", 2)) == [("A","A"), ("A","B"), ("B","B")]
assert list(I.combinations_with_replacement("", 0)) == [()]
Voorbeelden
import itertools as I
print(list(I.combinations_with_replacement("AB", 2)))
print(list(I.combinations_with_replacement([], 0)))
Resultaat
[('A', 'A'), ('A', 'B'), ('B', 'B')]
[()]Voor r=0 bestaat zelfs uit lege invoer één keuze zonder elementen, de lege tuple.