Python bietet direkte Methoden zum Finden von Permutationen und Kombinationen einer Sequenz. Diese Methoden sind im Paket itertools enthalten.
Permutation
Importieren Sie zunächst das Paket itertools, um die Permutationsmethode in Python zu implementieren. Diese Methode verwendet eine Liste als Eingabe und gibt eine Objektliste von Tupeln zurück, die alle Permutationen in Listenform enthalten.
Python3
Java-Stack
# A Python program to print all> # permutations using library function> from> itertools> import> permutations> # Get all permutations of [1, 2, 3]> perm> => permutations([> 1> ,> 2> ,> 3> ])> # Print the obtained permutations> for> i> in> list> (perm):> > print> (i)> |
>
>
Ausgabe:
(1, 2, 3) (1, 3, 2) (2, 1, 3) (2, 3, 1) (3, 1, 2) (3, 2, 1)>
Zeitkomplexität: O(n!), wobei n die Länge der Eingabeliste ist. Das liegt daran, dass es n gibt! Permutationen von n Elementen, und das Programm generiert und druckt sie alle.
Nebenraum: O(n!), da das Programm alle n speichern muss! Speichern Sie Permutationen im Speicher, bevor Sie sie ausdrucken. Insbesondere speichert die perm-Variable, die durch den Aufruf von permutations([1, 2, 3]) erstellt wird, alle n! Permutationen im Speicher als Liste.
Es erzeugt n! Permutationen, wenn die Länge der Eingabesequenz n ist.
Wenn Sie Permutationen der Länge L erhalten möchten, implementieren Sie dies auf diese Weise.
Python3
# A Python program to print all> # permutations of given length> from> itertools> import> permutations> # Get all permutations of length 2> # and length 2> perm> => permutations([> 1> ,> 2> ,> 3> ],> 2> )> # Print the obtained permutations> for> i> in> list> (perm):> > print> (i)> |
>
>
Ausgabe:
(1, 2) (1, 3) (2, 1) (2, 3) (3, 1) (3, 2)>
Die zeitliche Komplexität dieses Programms beträgt O(n^r), wobei n die Länge des Eingabearrays und r die Länge der zu generierenden Permutationen ist.
Die Raumkomplexität beträgt ebenfalls O(n^r), da alle Permutationen vor dem Drucken im Speicher gespeichert werden.
Es erzeugt nCr * r! Permutationen, wenn die Länge der Eingabesequenz n und der Eingabeparameter r ist.
Kombination
Diese Methode nimmt eine Liste und eine Eingabe r als Eingabe und gibt eine Objektliste von Tupeln zurück, die alle möglichen Kombinationen der Länge r in Listenform enthalten.
Python3
# A Python program to print all> # combinations of given length> from> itertools> import> combinations> # Get all combinations of [1, 2, 3]> # and length 2> comb> => combinations([> 1> ,> 2> ,> 3> ],> 2> )> # Print the obtained combinations> for> i> in> list> (comb):> > print> (i)> |
int, um Java zu char
>
>
Ausgabe:
(1, 2) (1, 3) (2, 3)>
1. Kombinationen werden in der lexikografischen Sortierreihenfolge der Eingabe ausgegeben. Wenn die Eingabeliste also sortiert ist, werden die Kombinationstupel in sortierter Reihenfolge erzeugt.
Python3
Linux welches
# A Python program to print all> # combinations of a given length> from> itertools> import> combinations> # Get all combinations of [1, 2, 3]> # and length 2> comb> => combinations([> 1> ,> 2> ,> 3> ],> 2> )> # Print the obtained combinations> for> i> in> list> (comb):> > print> (i)> |
>
>
Ausgabe:
(1, 2) (1, 3) (2, 3)>
2. Elemente werden aufgrund ihrer Position und nicht aufgrund ihres Wertes als einzigartig behandelt. Wenn also die Eingabeelemente eindeutig sind, gibt es in jeder Kombination keine Wiederholungswerte.
Python3
# A Python program to print all combinations> # of given length with unsorted input.> from> itertools> import> combinations> # Get all combinations of [2, 1, 3]> # and length 2> comb> => combinations([> 2> ,> 1> ,> 3> ],> 2> )> # Print the obtained combinations> for> i> in> list> (comb):> > print> (i)> |
>
>
Ausgabe:
(2, 1) (2, 3) (1, 3)>
3. Wenn wir eine Kombination desselben Elements mit demselben Element erstellen möchten, verwenden wir Kombinationen_mit_Ersatz.
CSS erstes Kind
Python3
# A Python program to print all combinations> # with an element-to-itself combination is> # also included> from> itertools> import> combinations_with_replacement> # Get all combinations of [1, 2, 3] and length 2> comb> => combinations_with_replacement([> 1> ,> 2> ,> 3> ],> 2> )> # Print the obtained combinations> for> i> in> list> (comb):> > print> (i)> |
>
>
Ausgabe:
(1, 1) (1, 2) (1, 3) (2, 2) (2, 3) (3, 3)>