๐Ÿฆœ Computer Language/Python

[Python] itertools: permutations, combinations | ์ˆœ์—ด, ์กฐํ•ฉ

permutations (์ˆœ์—ด)

iterable ๊ฐ์ฒด์—์„œ n๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฝ‘์•„ ์ผ๋ ฌ๋กœ ๋‚˜์—ดํ•˜๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ

 

product (์ค‘๋ณต์ˆœ์—ด)

iterable ๊ฐ์ฒด์—์„œ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์—ฌ n๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฝ‘์•„ ์ผ๋ ฌ๋กœ ๋‚˜์—ดํ•˜๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ

 

combinations  (์กฐํ•ฉ)

iterable ๊ฐ์ฒด์—์„œ n๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฝ‘์•„ ์ˆœ์„œ๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š๊ณ  ๋‚˜์—ดํ•˜๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ

 

combinations_with_replacement (์ค‘๋ณต์กฐํ•ฉ)

iterable ๊ฐ์ฒด์—์„œ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์—ฌ n๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฝ‘์•„ ์ˆœ์„œ๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š๊ณ  ๋‚˜์—ดํ•˜๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ 

 

์ด๋ฅผ ์‚ฌ์šฉํ•œ ์˜ˆ์‹œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค. 

 

from itertools import combinations, combinations_with_replacement, permutations, product

data = ['A','B','C']

permutations_result = list(permutations(data,2))
product_result = list(product(data, repeat=2))
combinations_result = list(combinations(data,2))
combinations_with_replacement_result = list(combinations_with_replacement(data,2))

print(permutations_result)
# [('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'C'), ('C', 'A'), ('C', 'B')]

print(product_result)
# [('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'B'), ('B', 'C'), ('C', 'A'), ('C', 'B'), ('C', 'C')]

print(combinations_result)
#[('A', 'B'), ('A', 'C'), ('B', 'C')]

print(combinations_with_replacement_result)
#[('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'B'), ('B', 'C'), ('C', 'C')]