Be Data
[파이썬 모듈] 리스트의 모든 조합 구하는 방법 본문
리스트의 모든 조합 구하는 방법에는 3가지가 있음
- product
- permutations
- combinations
하나의 리스트에서 모든 조합을 구할 때,
permutations, combinations 사용
from itertools import permutations
from itertools import combinations
items = ['a','b','c']
list(permutations(items,2)) # [('a','b'),('a','c'),('b','a'),('b','c'),('c,'a'),('c','b')]
list(combinations(items,2)) # [('a','b'),('a','c'),('b','c')]
두 개 이상의 리스트의 모든 조합 구할 때
from itertools import product
items = [['a','b'],['1'],['!','@']
list(product(*items)) # [('a','1','!'],('a','1','@') ...]
'IT > python' 카테고리의 다른 글
파이썬 Collections 모듈 (0) | 2022.10.29 |
---|---|
코테 보기 직전 (0) | 2022.10.29 |
<문자열 처리> 관련 함수들 - python (0) | 2022.09.20 |
[프로그래머스 ]숫자 문자열과 영단어 : replace() (0) | 2022.09.14 |
Comments