Let's Run Jinyeah

[Python] list 원소의 모든 조합 구하기 본문

Programming/Algorithm

[Python] list 원소의 모든 조합 구하기

jinyeah 2021. 11. 17. 01:01

1. 하나의 리스트에서 모든 조합 구하기 - itertools의 permutations 또는 combinations이용

from itertools import permutations

letters = ['a', 'b', 'c']
result = list(permutations(items, 2))
result = list(map(''.join, result))
# ['ab', 'ac', 'ba', 'bc', 'ca', 'cb']
from itertools import combinations

letters = ['a', 'b', 'c']
result = list(permutations(items, 2))
result = list(map(''.join, result))
# ['ab', 'ac', 'bc']

2. 두개 이상의 리스트에서 모든 조합 구하기 - itertools의 product 이용

from itertools import product
letters = [['a','b','c'], ['d','e','f']]
result = list(product(*letter_list))
result = list(map(''.join, result))

# ["ad","ae","af","bd","be","bf","cd","ce","cf"]

 

 

'Programming > Algorithm' 카테고리의 다른 글

Outer product, Inner product  (0) 2022.10.06
[Python] Data Structure - Tree  (0) 2021.10.19
[Python] 이진탐색  (0) 2021.10.19
[Python] 정렬 - 선택, 삽입, 퀵  (0) 2021.10.14
[Python] 기본 탐색 - 완전탐색, DFS/BFS  (0) 2021.10.14
Comments