Programming/Python

[Python] itertools 사용하기 (permutaions, product, combinations)

당닝 2021. 8. 9. 16:03
728x90
    • itertools: 경우의 수를 찾을 때 사용되는 라이브러리 (import itertools)
      • itertools.permutations: 순서는 중요하고 중복은 허용되지 않음.
        ex) itertools.permutations([1, 2, 3], 2) => [(1,2), (1,3), (2,1), (2,3), (3,1), (3,2)]
      • itertools.product: 순서는 중요하고 중복은 허용됨.
        ex) itertools.product([1, 2, 3], 2) => [(1,1), (1,2), (1,3), (2,1), (2,2), (2,3), (3,1), (3,2), (3,3)]
      • itertools.combinations: 순서는 중요하지 않고 중복은 허용되지 않음.
        ex) itertools.combinations([1, 2, 3], 2) => [(1,2), (1,3), (2,3)]
      • itertools.combinations_with_replacement: 순서는 중요하지 않고 중복은 허용됨.
        ex) itertools.combinations_with_replacement([1, 2, 3], 2) => [(1,1), (1,2), (1,3), (2,2), (2,3), (3,3)]
728x90