ror_coding

[Python] Permutations, Combination, Product (Itertools) 본문

Algorithm/Python

[Python] Permutations, Combination, Product (Itertools)

ro_rdil_31 2024. 12. 4. 14:59
728x90

 

from itertools import

  • permutations (순열)
  • combinations (조합)
  • product (주어진 반복 가능한 객체(iterable)들로 만들 수 있는 모든 조합 반환)
  • cycle

 

from itertools import permutations as perm # 순열
from itertools import combinations as comb # 조합
from itertools import product # 모든 가능한 조합
from itertools import cycle 

for p in perm(data): # 순열. (모든 경우의 수)

for c in comb(data, 3): # 3개씩 조합.

# product
l = [(x, -x) for x in numbers] # [(4, -4), (1, -1), (2, -2), (1, -1)]
s = list(map(sum, product(*l))) # *l는 위 list를 빼고 (4, -4) (1, -1)..

# product (완전탐색)
l1 = []
words = ['A','E','I','O','U']
for i in range(1,6): # 1~5
    for j in list(product(words, repeat=i)): # repeat=i를 하면 i==2일 때, 'AA', 'AE'..반복 저장.
        l1.append(''.join(j))

 

 

 

728x90

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

[Programmers] 튜플 - 64065  (0) 2024.12.04
[Python] re (정규표현식)  (0) 2024.12.04
[Programmers] 피로도 - 87946  (3) 2024.12.04
[Programmers] 의상 - 42578  (2) 2024.12.02
[Python] Stack, Queue (collections) (deque)  (0) 2024.12.02