Kode berikut dengan Python 2.6 dan di atas SAJA
Pertama, impor itertools
:
import itertools
Permutasi (masalah pesanan):
print list(itertools.permutations([1,2,3,4], 2))
[(1, 2), (1, 3), (1, 4),
(2, 1), (2, 3), (2, 4),
(3, 1), (3, 2), (3, 4),
(4, 1), (4, 2), (4, 3)]
Kombinasi (pesanan TIDAK masalah):
print list(itertools.combinations('123', 2))
[('1', '2'), ('1', '3'), ('2', '3')]
Produk Cartesian (dengan beberapa iterables):
print list(itertools.product([1,2,3], [4,5,6]))
[(1, 4), (1, 5), (1, 6),
(2, 4), (2, 5), (2, 6),
(3, 4), (3, 5), (3, 6)]
Produk Cartesian (dengan satu iterable dan sendiri):
print list(itertools.product([1,2], repeat=3))
[(1, 1, 1), (1, 1, 2), (1, 2, 1), (1, 2, 2),
(2, 1, 1), (2, 1, 2), (2, 2, 1), (2, 2, 2)]