반응형
이전 포스팅에서 C++, MATLAB 을 사용하여 Cartesian Product 를 구하는 방법에 대해 알아본 적이 있습니다.
2013/08/18 - [programming language/MATLAB] - MATLAB 모든 경우의 수 뽑기 Cartesian Product
2014/03/01 - [programming language/C/C++] - C++ Cartesian Product
2013/08/18 - [programming language/powershell] - Powershell 경우의 수 조합 다 구하기(Cartesian Product)
이번에는 python 에서 Cartesian Product 를 구하는 방법에 대해 확인해 보니 이전의 방법들 보다 훨씬 더 쉽더군요.
아래 주소에 그 방법이 나와 있었습니다.
http://stackoverflow.com/questions/9101101/python-cartesian-product
itertools.product() 함수를 사용하는 방식이며 Document 는 아래 주소에서 확인 할 수 있습니다.
https://docs.python.org/2/library/itertools.html#itertools.product
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
if __name__ == '__main__': | |
pass | |
letters = ( | |
('b','c'), | |
('a','e'), | |
('d','f') | |
) | |
for l in itertools.product(*letters): | |
print ''.join(l) | |
다음과 같은 결과가 나옵니다.
bad
baf
bed
bef
cad
caf
ced
cef
반응형
'programming language > Python' 카테고리의 다른 글
Python GraphViz 모듈 (0) | 2014.12.28 |
---|---|
윈도우용 Python 모듈 모음 (0) | 2014.12.27 |
Python C/C++ 주석 지우기 (0) | 2014.12.25 |
Python 을 사용한 MS Word Generation (0) | 2014.12.22 |
Python Computer Algebra System(CAS) package sympy (0) | 2014.12.03 |
Python Pyzo, IEP (0) | 2014.12.03 |
Python 배열의 인덱스도 보기 enumerate (0) | 2014.11.30 |
Python Recursive File Search, Delete (0) | 2014.11.30 |
댓글