Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

파이썬 잘하고 싶다

모듈로 순열,조합 구하기(python)(파이썬 잘하고 싶다) 본문

파이썬 문제풀이 팁

모듈로 순열,조합 구하기(python)(파이썬 잘하고 싶다)

파이썬 잘하고 싶다. 2020. 6. 13. 23:36

외장함수 itertools안에있는 combinations,permutations를 이용해서 배열의 조합을 구했다.

combinations(조합)

from itertools import permutations
n = int(input()) #n개의 순열
arr = list(map(int,input().split())) # 순열을 구할 배열
li = permutations(arr,n) #permutations(순열을 구할 배열, 구할개수)
for i in li:
    print(i)

 

permutations(순열)

from itertools import combinations
n = int(input()) #n개의 조합
arr = list(map(int,input().split())) # 조합을 구할 배열
li = combinations(arr,n) # combinations(조합을 구할배열, 조합 개수)
for i in li:
    print(i)
    

 

특수한경우 :

중복이 가능한경우

 

from itertools import combinations_with_replacement
n,m = map(int,input().split())
arr = [i+1 for i in range(n)]
for i in combinations_with_replacement(arr,m):
    print(*i)

'파이썬 문제풀이 팁' 카테고리의 다른 글

python DFS 런타임에러의 대부분 원인  (0) 2020.03.23