본문 바로가기

파이썬

[파이썬] combinations()

# combinations(nums, 3)이면 nums를 3개씩 나누어 따로 만든다.

from itertools import combinations 

 

nums = [1,2,7,6,4]

A = list(combinations(nums, 3))

 

print(A) - 

[(1, 2, 7), (1, 2, 6), (1, 2, 4), (1, 7, 6), (1, 7, 4), (1, 6, 4), (2, 7, 6), (2, 7, 4), (2, 6, 4), (7, 6, 4)]


# combinations의 두 번째 인자가 4인 경우

from itertools import combinations 

 

nums = [1,2,7,6,4]

A = list(combinations(nums, 4))

 

print(A) - 

[(1, 2, 7, 6), (1, 2, 7, 4), (1, 2, 6, 4), (1, 7, 6, 4), (2, 7, 6, 4)]