2016-12-28 2 views

Antwort

2

können Sie das itertools Modul verwenden:

>>> from itertools import permutations 
>>> s = set((1,2,3)) 
>>> list(permutations(s)) 

[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)] 

>>> list(permutations(s, 2)) 
[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)] 

UPDATE: Vielleicht ist mehr wie das, was gefragt wurde:

>>> from itertools import chain, combinations_with_replacement, permutations 
>>> list(set(chain.from_iterable(permutations(x) for x in combinations_with_replacement(s, 3)))) 
[(1, 3, 2), 
(1, 3, 1), 
(3, 3, 1), 
(1, 1, 1), 
(3, 3, 3), 
(2, 3, 2), 
(3, 3, 2), 
(2, 3, 3), 
(2, 3, 1), 
(3, 2, 2), 
(3, 1, 3), 
(3, 2, 3), 
(3, 1, 2), 
(1, 2, 1), 
(3, 1, 1), 
(3, 2, 1), 
(1, 2, 2), 
(1, 2, 3), 
(2, 1, 2), 
(2, 2, 3), 
(2, 1, 3), 
(2, 2, 2), 
(1, 1, 3), 
(2, 1, 1), 
(1, 1, 2), 
(2, 2, 1), 
(1, 3, 3)] 
+1

ich eine ähnliche Interpretation der Frage hatte. Aber 'liste (Permutationen (s, 3))' gibt '6' Listen zurück, aber @OP muss 'k^n' Listen (27)' zurückgeben. – shash678

Verwandte Themen