๋”ฅ๋Ÿฌ๋‹/Today I learned :

[๋”ฅ๋Ÿฌ๋‹] itertools ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

์ฃผ์˜ ๐Ÿฑ 2021. 4. 1. 10:25
728x90
๋ฐ˜์‘ํ˜•

 itertools ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

 

๋ฐ˜๋ณต ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜ ์ œ๊ณต

 

import itertools

list = [1,2,3,4,5]

 

์กฐํ•ฉ

list์š”์†Œ์˜ ์Œ์„ ์ถ”์ถœํ•˜์—ฌ ์ถœ๋ ฅํ•œ๋‹ค

for x in itertools.combinations(list,2):
	print(x)
(1,2)
(1,3)
(1,4)
(1,5)
(2,3)
(2,4)
(2,5)
(3,4)
(3,5)
(4,5)

 

ํ•˜๋‚˜์˜ ์—ฐ์†๋œ ๋ฐฐ์—ด๋กœ ๊ฒฐํ•ฉ

list์— a,b,c๋ฅผ  ๊ฒฐํ•ฉํ•˜๊ณ  ์š”์†Œ ๊ฐ’ ํ”„๋ฆฐํŠธ

 

for x in itertools.chain(list,['a','b'.'c']):
	print(x)
1
2
3
4
5
a
b
c
๋ฐ˜์‘ํ˜•