Language/Python

itertools.combinations

Mesut Özil 2024. 2. 5. 08:51

itertools.combinations

itertools.combinations은 itertools 모듈에서 제공되는 함수 중 하나로,

주어진 iterable(반복 가능한 객체)에서 가능한 모든 조합을 생성합니다.

여기에는 iterable에서 원소를 선택하여 지정된 길이 조합을 만드는 작업이 포함됩니다.

 

from itertools import combinations

my_list = [1, 2, 3, 4]

# 길이가 2인 조합 생성
combs = combinations(my_list, 2)

# 생성된 조합 출력
for comb in combs:
    print(comb)
    
# 결과  
(1, 2)
(1, 3)
(1, 4)
(2, 3)
(2, 4)
(3, 4)

 

 

 

본 게시글은 ChatGPT의 도움을 받아 작성하였습니다.