logo

Python의 순열과 조합

Python은 시퀀스의 순열과 조합을 찾는 직접적인 방법을 제공합니다. 이러한 방법은 itertools 패키지에 있습니다.

순열

먼저 itertools 패키지를 가져와 Python에서 순열 방법을 구현합니다. 이 메소드는 목록을 입력으로 사용하고 목록 형식의 모든 순열을 포함하는 튜플의 객체 목록을 반환합니다.



파이썬3






# 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)>



>

>

산출:

(1, 2, 3) (1, 3, 2) (2, 1, 3) (2, 3, 1) (3, 1, 2) (3, 2, 1)>

시간 복잡도: O(n!), 여기서 n은 입력 목록의 길이입니다. n이 있기 때문이죠! n개 요소의 순열을 생성하고 프로그램은 해당 요소를 모두 생성하고 인쇄합니다.
보조 공간: O(n!), 프로그램은 모든 n!을 저장해야 하기 때문입니다. 인쇄하기 전에 메모리에 순열을 적용합니다. 특히 permutations([1, 2, 3])를 호출하여 생성된 perm 변수는 모든 n!을 저장합니다. 메모리의 순열을 목록으로 저장합니다.

n을 생성합니다! 입력 시퀀스의 길이가 n인 경우 순열.
길이 L의 순열을 얻으려면 이 방법으로 구현하십시오.

Java의 간단한 날짜 포맷터

파이썬3




자바 char을 int로 변환

# 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)>

>

>

산출:

(1, 2) (1, 3) (2, 1) (2, 3) (3, 1) (3, 2)>

이 프로그램의 시간 복잡도는 O(n^r)입니다. 여기서 n은 입력 배열의 길이이고 r은 생성될 순열의 길이입니다.

인쇄하기 전에 모든 순열이 메모리에 저장되므로 공간 복잡도도 O(n^r)입니다.

nCr * r을 생성합니다! 입력 시퀀스의 길이가 n이고 입력 매개변수가 r인 경우 순열.

콤비네이션

이 메소드는 목록과 입력 r을 입력으로 사용하고 길이 r의 가능한 모든 조합을 목록 형식으로 포함하는 튜플의 객체 목록을 반환합니다.

파이썬3




# 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)>

C 프로그래밍의 문자열 배열
>

>

산출:

(1, 2) (1, 3) (2, 3)>

1. 조합은 입력의 사전식 정렬 순서로 방출됩니다. 따라서 입력 목록이 정렬되면 조합 튜플이 정렬된 순서로 생성됩니다.

파이썬3




# 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)>

문자열 함수 자바
>

>

산출:

(1, 2) (1, 3) (2, 3)>

2. 요소는 값이 아닌 위치에 따라 고유한 것으로 간주됩니다. 따라서 입력 요소가 고유한 경우 각 조합에 반복 값이 없습니다.

파이썬3




# 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)>

>

>

웹드라이버

산출:

(2, 1) (2, 3) (1, 3)>

3. 동일한 요소를 동일한 요소로 조합하려면 Combinations_with_replacement를 사용합니다.

파이썬3




# 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)>

>

>

산출:

(1, 1) (1, 2) (1, 3) (2, 2) (2, 3) (3, 3)>