CKTIS / Python
itertools.combinations_with_replacement
Wählt r Positionen mit nicht fallenden Indizes und erlaubt die Wiederwahl derselben Position.
Version: Python 3.14; repository image, production runtime not attested
Syntax
itertools.combinations_with_replacement(iterable, r)Wählt r Positionen mit nicht fallenden Indizes und erlaubt die Wiederwahl derselben Position.
Aus AB entstehen AA, AB und BB, während BA die Reihenfolge umkehren würde.
Für r=0 gibt es auch bei leerem Vorrat genau eine leere Auswahl als leeres Tupel.
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)) == [()]
Beispiele
import itertools as I
print(list(I.combinations_with_replacement("AB", 2)))
print(list(I.combinations_with_replacement([], 0)))
Ergebnis
[('A', 'A'), ('A', 'B'), ('B', 'B')]
[()]Für r=0 gibt es auch bei leerem Vorrat genau eine leere Auswahl als leeres Tupel.