본문 바로가기

코딩테스트 준비

파이썬 itertools permutations!

permutation= 순열

import itertools
n = int(input())
arr = [i for i in range(1, n+1)]
t = itertools.permutations(arr, n)
for i in t:
print(*i)

# 3
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

itertools 라이브러리의 permutations()를 사용하여 해당 리스트, 문자열의 원소들로 만든 순열들을 튜플 형태로 얻을 수 있다.