๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

IT96

[Python] ์ œ๋„ˆ๋ ˆ์ดํ„ฐ(Generator)_ํ”„๋กœ๊ทธ๋žจ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์„ธ๊ณ„๋ฅผ ํƒ๊ตฌํ•ฉ์‹œ๋‹ค. 1. ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ์ดํ„ฐ๋ ˆ์ดํ„ฐ(๋ฐ˜๋ณต)์˜ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ 2. ์ดํ„ฐ๋ ˆ์ดํ„ฐ์™€ ๋™์ผํ•˜๊ฒŒ ํ˜ธ์ถœ ์‹œ์ ์—๋งŒ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‚ฌ์šฉ์ด ๋๋‚˜๋ฉด ์†Œ๋ฉธ๋ฉ๋‹ˆ๋‹ค. 3. ๋ฐ˜ํ™˜ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ช…๋ น์–ด๋Š” return์ด ์•„๋‹Œ, yield๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. 4. ์ดํ„ฐ๋ ˆ์ดํ„ฐ์™€ ๋™์ผํ•˜๊ฒŒ ์ „์ฒด ๋˜๋Š” next() ํ•œ๊ฑด์”ฉ ์ถœ๋ ฅ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ๊ธฐ๋ณธ ๋ฌธ๋ฒ• def simple_generator() : ### ์ฒซ๋ฒˆ์งธ ๋ฐ˜ํ™˜ํ•  ๊ฐ’ yield 1 ### ๋‘๋ฒˆ์งธ ๋ฐ˜ํ™˜ํ•  ๊ฐ’ yield 2 ### ์„ธ๋ฒˆ์งธ ๋ฐ˜ํ™˜ํ•  ๊ฐ’ yield 3 ### ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ์ „์ฒด ์ถœ๋ ฅํ•˜๊ธฐ # - ์ œ๋„ˆ๋ ˆ์ดํ„ฐ์—์„œ๋Š” ํ•จ์ˆ˜๋ฅผ ๊ฐ์ฒด๋กœ ์ธ์ง€ํ•˜๊ณ  ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. # - ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•œ๋‹ค๋Š” ์˜๋ฏธ ๋˜๋Š” ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค๋Š” ์˜๋ฏธ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. for v in simple.. 2023. 11. 21.
[Python] 3. ๋„์„œ ์ž…๊ณ /๋Œ€์ถœ/๋ฐ˜๋‚ฉ ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•œ ํ‚ค์˜ค์Šคํฌ ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋žจ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ์‹ค์Šต (ํšŒ์›๊ฐ€์ž… ๊ธฐ๋Šฅ ์ถ”๊ฐ€) ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์„ธ๊ณ„๋ฅผ ํƒ๊ตฌํ•ฉ์‹œ๋‹ค. * class ์‚ฌ์šฉํ•œ ์‹ค์Šต์€ ์•„๋ž˜ ๊ฒŒ์‹œ๊ธ€ ์ฐธ๊ณ  ๋ฐ”๋ž๋‹ˆ๋‹ค. 2023.11.14 - [IT/Python] - [Python] ๋„์„œ ์ž…๊ณ /๋Œ€์ถœ/๋ฐ˜๋‚ฉ ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•œ ํ‚ค์˜ค์Šคํฌ ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋žจ ์‹ค์Šต (class ์ด์šฉ) [Python] ๋„์„œ ์ž…๊ณ /๋Œ€์ถœ/๋ฐ˜๋‚ฉ ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•œ ํ‚ค์˜ค์Šคํฌ ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋žจ ์‹ค์Šต (class ์ด์šฉ) ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์„ธ๊ณ„๋ฅผ ํƒ๊ตฌํ•ฉ์‹œ๋‹ค. ๋„์„œ ์ž…๊ณ /๋Œ€์ถœ/๋ฐ˜๋‚ฉ ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•œ ํ‚ค์˜ค์Šคํฌ ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋žจ ์‹ค์Šต [๋ฏธ์…˜] ๋„์„œ๋ชฉ๋ก : ์ƒˆ๋กœ์šด ์ฑ…์ด ๋„์„œ๊ด€์— [์ž…๊ณ ]๋˜์—ˆ์„ ๋•Œ ํ”„๋กœ๊ทธ๋žจ ์ž‘์„ฑ ๋„์„œ๋Œ€์ถœ : ๋„์„œ ๋ชฉ๋ก ityranno.tistory.com * decorator ์‚ฌ์šฉํ•œ ์‹ค์Šต์€ ์•„๋ž˜ ๊ฒŒ์‹œ๊ธ€ ์ฐธ๊ณ  ๋ฐ”๋ž๋‹ˆ๋‹ค. 2023.11.16 - [IT/Python] - [Python] 2. ๋„์„œ ์ž…๊ณ /.. 2023. 11. 20.
[Python] ํŒŒ์ด์ฌ ์ •๊ทœํ‘œํ˜„์‹ ํŒจํ„ด ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์„ธ๊ณ„๋ฅผ ํƒ๊ตฌํ•ฉ์‹œ๋‹ค. * ๋Œ€๊ด„ํ˜ธ([]) : ๋Œ€๊ด„ํ˜ธ ์•ˆ์— ์žˆ๋Š” ๋ฌธ์ž ์ค‘์— ํ•˜๋‚˜๋ผ๋„ ๋งค์น˜๋˜๋ฉด ์ถ”์ถœ * ์ (.) : ์•ž๋’ค ๋ฌธ์ž์˜ ์‚ฌ์ด์— ๋ณดํ†ต ์ฃผ๋กœ ์‚ฌ์šฉํ•จ. ํ•˜๋‚˜์˜ ๋ฌธ์ž๋ฅผ ์˜๋ฏธํ•จ : ์˜ˆ์‹œ -> a.c์˜ ํŒจํ„ด์ธ ๊ฒฝ์šฐ "abc", "adc".... * ๋ฐ˜๋ณต(*) : ์•ž์˜ ๋ฌธ์ž๊ฐ€ 0๋ฒˆ ์ด์ƒ ๋ฐ˜๋ณต๋  ๋•Œ ์ถ”์ถœ : ์˜ˆ์‹œ -> ab*c์˜ ํŒจํ„ด์ธ ๊ฒฝ์šฐ "ac", "abc", "abbc", "abbbbbbbbbc"..... * ๋ฐ˜๋ณต(+) : ์•ž์˜ ๋ฌธ์ž๊ฐ€ 1๋ฒˆ ์ด์ƒ ๋ฐ˜๋ณต๋  ๋•Œ ์ถ”์ถœ : ์˜ˆ์‹œ -> ab+c์˜ ํŒจํ„ด์ธ ๊ฒฝ์šฐ "abc", "abbc", "abbbbbbc"...... * ๋ฐ˜๋ณต ({m, n}) : ์•ž์˜ ์ˆซ์ž๊ฐ€ ์ตœ์†Œ m๋ฒˆ, ์ตœ๋Œ€ n๋ฒˆ ๋ฐ˜๋ณต๋  ๋•Œ ์ถ”์ถœ๋จ : ์˜ˆ์‹œ -> a{2, 4}์˜ ํŒจํ„ด์ธ ๊ฒฝ์šฐ "aa", "aaa",.. 2023. 11. 20.
[Python] ๋ฌธ์ž์—ด์„ ์ „๋‹ฌ ๋ฐ›์•„์„œ ๋ฌธ์ž ํ•˜๋‚˜์”ฉ ์ถ”์ถœํ•˜์—ฌ ๋ณ€ํ™˜ํ•˜๋Š” ์ดํ„ฐ๋ ˆ์ดํ„ฐ(Iterator) ์ƒ์„ฑํ•˜๊ธฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์„ธ๊ณ„๋ฅผ ํƒ๊ตฌํ•ฉ์‹œ๋‹ค. ๋ฌธ์ž์—ด์„ ์ „๋‹ฌ ๋ฐ›์•„์„œ ๋ฌธ์ž ํ•˜๋‚˜์”ฉ ์ถ”์ถœํ•˜์—ฌ ๋ณ€ํ™˜ํ•˜๋Š” ์ดํ„ฐ๋ ˆ์ดํ„ฐ(Iterator) ์ƒ์„ฑํ•˜๊ธฐ ### ์ดํ„ฐ๋ ˆ์ดํ„ฐ ํด๋ž˜์Šค ์ƒ์„ฑํ•ด์„œ Hello ๊ฐ ๋‹จ์–ด ์ถœ๋ ฅํ•˜๊ธฐ # - ํด๋ž˜์Šค ์ด๋ฆ„ : StringIterator # - ์ž„์˜ ๋ฌธ์ž์—ด์„ ๋ฐ›์•„์„œ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. # - ์ž„์˜ ๋ฌธ์ž์—ด์€ ์™ธ๋ถ€์—์„œ ํด๋ž˜์Šค ์ƒ์„ฑ์‹œ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค. class StringIterator: ### ํด๋ž˜์Šค ์ƒ์„ฑ์ž def __init__(self, p_text) : # - nextํ•จ์ˆ˜์—์„œ 1์”ฉ ์ฆ๊ฐ€์‹œํ‚ค๋ฉด์„œ ๋ฐ˜๋ณต ์กฐ๊ฑด์— ์‚ฌ์šฉํ•  ๋ณ€์ˆ˜ self.index = 0 # - ๋ฐ›์•„์˜จ ๋ฌธ์ž์—ด self.text = p_text ### ๋ฐ˜๋ณต์ˆ˜ํ–‰์„ ์œ„ํ•œ iter ํ•จ์ˆ˜ ์ •์˜ def __iter__(self) : return self ### ํ•œ๊ฑด ํ•œ๊ฑด .. 2023. 11. 18.

loading