Baza znanjaCentar za ispite
HRHrvatski
Odaberi jezik13

CKTIS / Python

itertools.combinations_with_replacement

Bira r položaja s neopadajućim indeksima i dopušta ponavljanje istog položaja.

Verzija: Python 3.14; repository image, production runtime not attested

Sintaksa

itertools.combinations_with_replacement(iterable, r)

Bira r položaja s neopadajućim indeksima i dopušta ponavljanje istog položaja.

Iz AB nastaju AA, AB i BB, dok bi BA preokrenuo redoslijed.

Za r=0 i uz prazan ulaz postoji jedan izbor bez elemenata, prazna n-torka.

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)) == [()]

Primjeri

import itertools as I
print(list(I.combinations_with_replacement("AB", 2)))
print(list(I.combinations_with_replacement([], 0)))

Rezultat

[('A', 'A'), ('A', 'B'), ('B', 'B')]
[()]

Za r=0 i uz prazan ulaz postoji jedan izbor bez elemenata, prazna n-torka.

Izvori