Notice
Recent Posts
Recent Comments
Link
«   2025/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
Tags
more
Archives
Today
Total
관리 메뉴

Be Data

[파이썬 모듈] 리스트의 모든 조합 구하는 방법 본문

IT/python

[파이썬 모듈] 리스트의 모든 조합 구하는 방법

bs engineer 2022. 9. 21. 12:52

리스트의 모든 조합 구하는 방법에는 3가지가 있음

  1. product
  2. permutations
  3. 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