์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ)

 

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/28

 

[python ์‹ฌํ™”] 11. ํด๋ž˜์Šค Getter, Setter, ์ถ”์ƒํด๋ž˜์Šค , class ABC

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ) <์ด์ „ ๊ธ€> https://silvercoding.tistory.com/27 https://silvercoding.tistory.com/26 https://silvercoding.tistory.com/25 ์ด๋ฒˆ ํฌ์ŠคํŠธ์—์„œ๋Š” ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์•Œ์•„..

silvercoding.tistory.com

* ์ด๋ฒˆ ํฌ์ŠคํŒ… ๋ถ€ํ„ฐ ๊ณต๋ถ€ํ•  ๊ฒƒ : 1. ๋ฐ˜๋ณตํ˜• ๊ฐ์ฒด์˜ ๋‚ด๋ถ€์ ์œผ๋กœ iter ํ•จ์ˆ˜ ์‹คํ–‰ ๋‚ด์šฉ, 2. ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ๋™์ž‘ ์›๋ฆฌ, 3. yield from

์ด ์ค‘์—์„œ๋„ ์˜ค๋Š˜์€ 1~2 ๋ฅผ ํฌ์ŠคํŒ… ํ•œ๋‹ค.  

 

 

- ํŒŒ์ด์ฌ ๋ฐ˜๋ณตํ˜• ์ข…๋ฅ˜ 

: for, collections, text file, List, Dict, Set, Tuple, unpacking, *args

 ---> ๋ฐ˜๋ณต์ด ๊ฐ€๋Šฅํ•œ ์ด์œ ? -> iter(x) ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๊ธฐ ๋•Œ๋ฌธ 

 

 

 

- ๋ฐ˜๋ณต๋ฌธ ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ 

t = 'ABCDEF'

 

* for ์‚ฌ์šฉ 

for c in t: # ์‹œํ€€์Šคํ˜•์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜๋ณต์ด ๊ฐ€๋Šฅํ•˜๋‹ค. 
    print('EX1-1 - ', c)

 EX1-1 -  A 
 EX1-1 -  B 
 EX1-1 -  C 
 EX1-1 -  D 
 EX1-1 -  E 
 EX1-1 -  F 

 

 

 

* while ์‚ฌ์šฉ 

w = iter(t)
print(type(w))

<class 'str_iterator'>

while True:
    try:
        print('EX1-2 - ', next(w))

    except StopIteration: # ๋ฌธ์ž๊ฐ€ ๋”์ด์ƒ ์—†๋Š”๋ฐ ํ˜ธ์ถœํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ 
        break

 EX1-2 -  A 
 EX1-2 -  B 
 EX1-2 -  C 
 EX1-2 -  D 
 EX1-2 -  E 
 EX1-2 -  F 

iterator type์œผ๋กœ ๋ณ€๊ฒฝํ•˜๊ณ  , next๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ while๋ฌธ์œผ๋กœ ๋ฐ˜๋ณต์„ ์‚ฌ์šฉํ•˜์˜€๋‹ค. 

 

 

from collections import abc

# ๋ฐ˜๋ณตํ˜• ํ™•์ธ 
print('EX1-3 - ', hasattr(t, '__iter__'))  # ---> t๊ฐ€ __iter__ ์˜ ์†์„ฑ์„ ๊ฐ–๊ณ  ์žˆ๋‹ˆ ? 
print('EX1-4 - ', isinstance(t, abc.Iterable)) # t๊ฐ€ abc์˜ Iterableํด๋ž˜์Šค์™€ ๊ฐ™์€ ์ธ์Šคํ„ด์Šค๋ƒ ? ---> True --> ์ˆœํšŒ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค. 
# dir() ๋กœ๋„ ํ™•์ธ ๊ฐ€๋Šฅ

 EX1-3 -  True 
 EX1-4 -  True 

๋ฐ˜๋ณตํ˜• ํƒ€์ž…์ธ์ง€ ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด ์œ„์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. (hasattr(), isinstance(), dir())

 

 

 

 

 

 

- next ๋ฉ”์†Œ๋“œ ์ง์ ‘ ๊ตฌํ˜„ ์˜ˆ์ œ 

class WordSplitIter: 
    def __init__(self, text):
        self._idx = 0 
        self._text = text.split(' ')

    def __next__(self):
        # print('Called __next__')
        try:
            word = self._text[self._idx]
        except IndexError:
            raise StopIteration('stop !')
        self._idx += 1
        return word

    def __iter__(self):
        print('Called __iter__')
        return self 

    def __repr__(self):
        return 'WordSplit(%s)' % (self._text)

๋ฌธ์žฅ์„ ์ž…๋ ฅํ•˜๋ฉด ๋„์–ด์“ฐ๊ธฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ splitํ•ด์„œ ๋ฆฌ์ŠคํŠธ์— ๋„ฃ์–ด์ค€ _text๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. __next__๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ํ•œ ๋‹จ์–ด์”ฉ ๋‚˜์˜ค๋„๋ก ๊ตฌํ˜„ํ•œ๋‹ค. 

 

* ๊ฐ์ฒด ์ƒ์„ฑ 

wi = WordSplitIter('who says the nights are for sleeping')

* ์‚ฌ์šฉ

print('EX2-1 - ', wi)
print('EX2-2 - ', next(wi))
print('EX2-3 - ', next(wi))
print('EX2-4 - ', next(wi))
print('EX2-5 - ', next(wi))
print('EX2-6 - ', next(wi))
print('EX2-7 - ', next(wi))
print('EX2-8 - ', next(wi))
# print('EX2-9 - ', next(wi))

 EX2-1 -  WordSplit(['who', 'says', 'the', 'nights', 'are', 'for', 'sleeping']) 
 EX2-2 -  who 
 EX2-3 -  says 
 EX2-4 -  the 
 EX2-5 -  nights 
 EX2-6 -  are 
 EX2-7 -  for 
 EX2-8 -  sleeping 

 

 

์ฃผ์„์„ ์ œ๊ฑฐํ•˜๊ณ  ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค. 

 Traceback (most recent call last): 
  File "c:/Users/sooki/Desktop/python_high/chapter06_01.py", line 55, in __next__       
    word = self._text[self._idx] 
 IndexError: list index out of range 

 During handling of the above exception, another exception occurred: 

 Traceback (most recent call last): 
  File "c:/Users/sooki/Desktop/python_high/chapter06_01.py", line 78, in <module>       
    print('EX2-9 - ', next(wi)) 
  File "c:/Users/sooki/Desktop/python_high/chapter06_01.py", line 57, in __next__       
    raise StopIteration('stop !') 
 StopIteration: stop ! 

๊ตฌํ˜„ํ•ด ๋†“์•˜๋˜ ์—๋Ÿฌ๊ฐ€ ๋‚˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ์—๋Ÿฌ๋ฅผ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

 

 

 

 

- Generator ํŒจํ„ด 

1. ์ง€๋Šฅํ˜• ๋ฆฌ์ŠคํŠธ, ๋”•์…”๋„ˆ๋ฆฌ, ์ง‘ํ•ฉ -> ๋ฐ์ดํ„ฐ ์…‹์ด ์ฆ๊ฐ€ ํ•  ๊ฒฝ์šฐ ๋ฉ”๋ชจ๋ฆฌ์˜ ์‚ฌ์šฉ๋Ÿ‰์ด ์ฆ๊ฐ€ํ•œ๋‹ค. -> ์ด ์ ์„ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๊ฐ€ ์™„ํ™”ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค. 

2. ๋‹จ์œ„ ์‹คํ–‰์ด ๊ฐ€๋Šฅํ•œ ์ฝ”๋ฃจํ‹ด (Coroutine) ๊ตฌํ˜„์— ์•„์ฃผ ์ค‘์š”ํ•˜๋‹ค. 

3. ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋”•์…”๋„ˆ๋ฆฌ , ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•œ ๋ฒˆ ํ˜ธ์ถœํ•  ๋•Œ๋งˆ๋‹ค ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ ๋ฆฌํ„ดํ•œ๋‹ค. <- ๋”ฐ๋ผ์„œ ์•„์ฃผ ์ž‘์€ ๋ฉ”๋ชจ๋ฆฌ ์–‘์„ ํ•„์š”๋กœ ํ•˜๊ฒŒ ๋œ๋‹ค. (๋ชจ๋“  ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๋‹ค ์ €์žฅํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, next๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”๋ชจ๋ฆฌ ์ฐจ์ง€๋Ÿ‰์ด ์ ์€ ๊ฒƒ)

 

class WordSplitGenerator: 
    def __init__(self, text):
        self._text = text.split(' ')

    def __iter__(self):
        for word in self._text:
            yield word # ์ œ๋„ˆ๋ ˆ์ดํ„ฐ 
        return

    def __repr__(self):
        return 'WordSplit(%s)' % (self._text)

์œ„์—์„œ ํ–ˆ๋˜ WordSplitIterํด๋ž˜์Šค์™€ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ํ•˜๋ฉด์„œ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋ฅผ ์ด์šฉํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค. ์ด ๊ฒฝ์šฐ์—๋Š” idx๋ณ€์ˆ˜๋„ ํ•„์š”๊ฐ€ ์—†์–ด์ง„๋‹ค. ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•˜์—ฌ yield ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค. next๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด์„œ yield๊ฐ€ ์‹คํ–‰๋œ ํ›„ ๋ฉˆ์ถ˜๋‹ค. ๋˜ ๋‹ค์‹œ next๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด ๊ธฐ์–ตํ•ด ๋†“์€ ๋ถ€๋ถ„๋ถ€ํ„ฐ ์‹คํ–‰๋˜๋„๋ก ํ•ด์ค€๋‹ค. 

 

 

*๊ฐ์ฒด ์ƒ์„ฑ

wg = WordSplitGenerator('who says the nights are for sleeping')
wt = iter(wg)

๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์ฃผ๊ณ , iter()ํ•จ์ˆ˜์— ๊ฐ์ฒด๋ฅผ ๋„ฃ์–ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. 

print('EX3-1 - ', wt)

EX3-1 -  <generator object WordSplitGenerator.__iter__ at 0x00000218C0E87570>

์ด๋ ‡๊ฒŒ ๋ฐ˜๋ณต์ด ๊ฐ€๋Šฅํ•œ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํƒ€์ž…์ธ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. 

 

print('EX3-2 - ', next(wt))
print('EX3-3 - ', next(wt))
print('EX3-4 - ', next(wt))
print('EX3-5 - ', next(wt))
print('EX3-6 - ', next(wt))
print('EX3-7 - ', next(wt))
print('EX3-8 - ', next(wt))
# print('EX3-9 - ', next(wt))  # ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ์ž๋™

 EX3-2 -  who 
 EX3-3 -  says 
 EX3-4 -  the 
 EX3-5 -  nights 
 EX3-6 -  are 
 EX3-7 -  for 
 EX3-8 -  sleeping 

์œ„์—์„œ๋Š” ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ํ•˜์˜€๋Š”๋ฐ, ์ด๋ ‡๊ฒŒ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋ฅผ ํ™œ์šฉํ•˜๋ฉด ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋Š” ์ž๋™์œผ๋กœ ๋œ๋‹ค. 

 

 

 

 

 

- Generator ์˜ˆ์ œ1 

* Generator : yield๋ฅผ ๋งŒ๋‚˜๋ฉด ๋ฉˆ์ถฐ์žˆ๋‹ค๊ฐ€ next๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๊ทธ ๋‹ค์Œ์„ ์‹คํ–‰ํ•จ 

def generator_ex1():
    print('start!')
    yield 'AAA'
    print('continue')
    yield 'BBB'
    print('end')

yield๋กœ Generator๋ฅผ ๊ตฌํ˜„ํ•œ ํ•จ์ˆ˜์ด๋‹ค. 

 

temp = iter(generator_ex1())

print('EX4-1 - ', next(temp))
print('EX4-2 - ', next(temp))
print('EX4-3 - ', next(temp))

 start! 
 EX4-1 -  AAA 
 continue 
 EX4-2 -  BBB 
 end 
 Traceback (most recent call last): 
  File "c:/Users/sooki/Desktop/python_high/chapter06_01.py", line 136, in <module>      
    print('EX4-3 - ', next(temp)) 
 StopIteration 

yield๋ฅผ ๋งŒ๋‚˜๋ฉด ๋ฉˆ์ถ”์–ด ์žˆ๋‹ค๊ฐ€ next๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด ์‹คํ–‰๋˜๋Š” ํ˜•ํƒœ์ด๋‹ค. 4-3์ฒ˜๋Ÿผ next๋ฅผ ๋˜ ํ•˜๋ฉด yield๊ฐ€ ๋”์ด์ƒ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค. 

 

 

 

* for, while๊ณผ ๊ฐ™์€ ๋ฐ˜๋ณต๋ฌธ์—์„œ ์‚ฌ์šฉ์ด ์ ์ ˆํ•จ 

for v in generator_ex1():
    print('EX4-3 - ', v)  # next๋ฅผ ๋‚ด๋ถ€์ ์œผ๋กœ ํ˜ธ์ถœ, StopIteration์—๋Ÿฌ๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ์žก์•„์คŒ

 start! 
 EX4-3 -  AAA 
 continue 
 EX4-3 -  BBB 
 end 

for๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด next๊ฐ€ ๋‚ด๋ถ€์ ์œผ๋กœ ํ˜ธ์ถœ๋˜๋ฉฐ, ์—๋Ÿฌ๋„ ๋‚ด๋ถ€์ ์œผ๋กœ ์žก์•„์ง€๋ฏ€๋กœ ์งง์€ ์ฝ”๋“œ๋กœ ๊น”๋”ํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

 

 

 

 

- Generator ์˜ˆ์ œ2 

temp2 = [x * 3 for x in generator_ex1()]
temp3 = (x * 3 for x in generator_ex1())

์ง€๋Šฅํ˜• ๋ฆฌ์ŠคํŠธ์™€ ํŠœํ”Œ์„ ์ƒ์„ฑํ•œ๋‹ค. ์ด ๊ฒฝ์šฐ์— ํŠœํ”Œ์€ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๊ฐ€ ๋œ๋‹ค. 

print('EX5-1 - ', temp2) # ์ด๋ฏธ ๋ฉ”๋ชจ๋ฆฌ์— ๋งŒ๋“ค์–ด์„œ ์˜ฌ๋ฆฐ ๊ฒƒ 
print('EX5-2 - ', temp3) # ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ์ƒ์„ฑ. ์•„์ง ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ๊ฐ€ ์žˆ์ง€ ์•Š์Œ. next๊ฐ€ ํ˜ธ์ถœ๋˜์–ด์•ผ๋งŒ ๋งŒ๋“ค์–ด์ง

 EX5-1 -  ['AAAAAAAAA', 'BBBBBBBBB'] 
 EX5-2 -  <generator object <genexpr> at 0x000001B3528D66D8> 

5-1์˜ list comprehension์€ ๋ฉ”๋ชจ๋ฆฌ์— ์ด๋ฏธ ํ• ๋‹น๋˜์–ด ์˜ฌ๋ผ๊ฐ€ ์žˆ๋Š” ๊ฒƒ์„ ๋ฐ˜ํ™˜ํ•œ ๊ฒƒ์ด๊ณ , ์ œ๋„ˆ๋ ˆ์ดํ„ฐ์ธ 5-2 ๋Š” ์•„์ง ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ๊ฐ€ ์žˆ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๊ณ  , next๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ ๋งŒ๋“ค์–ด์ง„๋‹ค. (๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด ์ ์€ ์ด์œ ) 

 

for i in temp2:
    print('EX5-3 - ', i)

 EX5-3 -  AAAAAAAAA 
 EX5-3 -  BBBBBBBBB 

๋ฆฌ์ŠคํŠธ ํ˜•์ด๋ฏ€๋กœ for๋ฌธ์œผ๋กœ ๋ฐ˜๋ณตํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

for i in temp3:  # ๋น…๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•  ๋•Œ๋Š” ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋กœ ๋ฐ˜๋ณต๋ฌธ ๋Œ๋ฆฌ๋Š” ๊ฒƒ์„ ๊ถŒ์žฅ 
    print('EX5-4 - ', i)

 start! 
 EX5-4 -  AAAAAAAAA 
 continue 
 EX5-4 -  BBBBBBBBB 
 end 

๊ฒฐ๊ณผ๋Š” ๋น„์Šทํ•ด๋ณด์ด์ง€๋งŒ ๊ณผ์ •๊ณผ ๋ฉ”๋ชจ๋ฆฌ ์ฐจ์ง€๋Ÿ‰์€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์ ํ•ฉํ•œ ๋ฐฉ๋ฒ•์„ ์“ฐ๋„๋ก ํ•˜์ž! 

 

 

 

 

 

 

- Generator ์˜ˆ์ œ3 (์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜) 

Generator ์˜ˆ์ œ 3 ์—์„œ๋Š” itertools๋ฅผ ์ด์šฉํ•œ Generator ์‚ฌ์šฉ์„ ํ•ด๋ณธ๋‹ค! 

import itertools

์šฐ์„  itertools ๋ฅผ import ํ•œ๋‹ค.

 

 

* ๋ฌดํ•œ (๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด ๋†“์ง€ ์•Š๋Š” ๋‹ค๋Š” ๊ฒƒ ! ) - itertools.count

gen1 = itertools.count(1, 2.5)

itertools.count(๊ธฐ๋ณธ๊ฐ’, ์ฆ๊ฐ€๊ฐ’)์„ ์ž‘์„ฑํ•œ๋‹ค. 

 

print('EX6-1 - ', next(gen1))
print('EX6-2 - ', next(gen1))
print('EX6-3 - ', next(gen1))
print('EX6-4 - ', next(gen1))

 EX6-1 -  1 
 EX6-2 -  3.5 
 EX6-3 -  6.0 
 EX6-4 -  8.5 

์œ„์™€ ๊ฐ™์ด 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ next๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด 2.5์”ฉ ์ฆ๊ฐ€์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. ๋ฌดํ•œ์œผ๋กœ ์ฆ๊ฐ€์‹œํ‚ฌ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด ๋†“์ง€ ์•Š๊ณ  next๊ฐ€ ํ˜ธ์ถœ๋˜๊ธฐ ์ „๊นŒ์ง€๋Š” ๋ฉ”๋ชจ๋ฆฌ์— ํ• ๋‹น๋˜์–ด ์žˆ์ง€ ์•Š๋‹ค. 

 

# for v in gen1:
#     print(v)

for๋ฌธ์œผ๋กœ ์ถœ๋ ฅํ•˜๋ฉด ๋ฌดํ•œ์œผ๋กœ 2.5์”ฉ ์ฆ๊ฐ€ํ•˜๊ฒŒ ๋œ๋‹ค. 

 

 

 

 

 

* ์กฐ๊ฑด - itertools.takewhile

gen2 = itertools.takewhile(lambda n: n < 10, itertools.count(1, 2.5))

์กฐ๊ฑด์„ ๊ฑธ ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜์ด๋‹ค. ์ด ๊ฒฝ์šฐ์—๋Š” ๊ฐ’์ด 10 ์ดํ•˜์ผ ๋•Œ๋งŒ 2.5์”ฉ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค. 

 

for v in gen2:
    print('EX6-5 - ', v)

EX6-5 -  1
EX6-5 -  3.5
EX6-5 -  6.0
EX6-5 -  8.5

 

 

 

 

 

* ํ•„ํ„ฐ ๋ฐ˜๋Œ€ - itertools.filterfalse

gen3 = itertools.filterfalse(lambda n: n < 3, [1, 2, 3, 4, 5])

filterfalseํ•จ์ˆ˜๋Š” ์กฐ๊ฑด์„ ๊ฑธ์–ด๋†“๊ณ  ์กฐ๊ฑด๊ณผ ๋ฐ˜๋Œ€๋˜๋Š” ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ํ•œ๋‹ค. 

for v in gen3:
    print('EX6-6 - ', v)

 EX6-6 -  3 
 EX6-6 -  4 
 EX6-6 -  5 

3๋ฏธ๋งŒ์ธ ์ˆ˜๋กœ ์กฐ๊ฑด์„ ๊ฑธ์–ด๋†“์•˜์œผ๋ฏ€๋กœ ๋ฐ˜๋Œ€๋˜๋Š” 3, 4, 5 ๊ฐ€ ์ถœ๋ ฅ๋˜์—ˆ๋‹ค. 

 

 

 

 

 

* ๋ˆ„์  ํ•ฉ๊ณ„ - itertools.accumulate

gen4 = itertools.accumulate([x for x in range(1, 11)])

accumulate๋Š” ๋ˆ„์ ํ•ด์„œ ๋”ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜์ด๋‹ค. 

for v in gen4:
    print('EX6-7 - ', v)

 EX6-7 -  1 
 EX6-7 -  3 
 EX6-7 -  6 
 EX6-7 -  10 
 EX6-7 -  15 
 EX6-7 -  21 
 EX6-7 -  28 
 EX6-7 -  36 
 EX6-7 -  45 
 EX6-7 -  55 

1๋ถ€ํ„ฐ 10๊นŒ์ง€ ๋ˆ„์ ์œผ๋กœ ๋”ํ•ด์กŒ๋‹ค. 

 

 

 

 

 

* ์—ฐ๊ฒฐ1 - itertools.chain

gen5 = itertools.chain('ABCDE', range(1, 11, 2))

chain์€ ์ธ์ž๊ฐ’๋“ค์„ ์—ฐ๊ฒฐํ•ด์ค€๋‹ค. 

print('EX5-8 - ', list(gen5))

 EX5-8 -  ['A', 'B', 'C', 'D', 'E', 1, 3, 5, 7, 9] 

 

 

 

 

 

 

* ์—ฐ๊ฒฐ2 - itertools.chain

gen6 = itertools.chain(enumerate('ABCDE'))

enumerate๋ฅผ ์ด์šฉํ•˜์—ฌ ์ธ๋ฑ์Šค์™€ ๋ฌถ์–ด์ค„ ์ˆ˜๋„ ์žˆ๋‹ค. 

print('EX6-9 - ', list(gen6))

 EX6-9 -  [(0, 'A'), (1, 'B'), (2, 'C'), (3, 'D'), (4, 'E')] 

ํŠœํ”Œ๋กœ ๋ฌถ์ธ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

 

 

 

* ๊ฐœ๋ณ„ - itertools.product

gen7 = itertools.product('ABCDE')

๊ฐœ๋ณ„์„ ํŠœํ”Œ ์•ˆ์— ๋„ฃ์–ด์ค„ ์ˆ˜๋„ ์žˆ๋‹ค. 

print('EX6-10 - ', list(gen7))

 EX6-10 -  [('A',), ('B',), ('C',), ('D',), ('E',)] 

 

 

 

 

 

 

 

* ์—ฐ์‚ฐ - itertools.product

gen8 = itertools.product('ABCDE', repeat=2) # ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋ฌถ์–ด์ค€๋‹ค.

repeat=2๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์˜ ์ˆ˜๋กœ  2๊ฐœ์”ฉ ๋ฌถ์–ด์ค€๋‹ค. 

print('EX6-11 - ', list(gen8))

 EX6-11 -  [('A', 'A'), ('A', 'B'), ('A', 'C'), ('A', 'D'), ('A', 'E'), ('B', 'A'), ('B', 'B'), ('B', 'C'), ('B', 'D'), ('B', 'E'), ('C', 'A'), ('C', 'B'), ('C', 'C'), ('C', 'D'), ('C', 'E'), ('D', 'A'), ('D', 'B'), ('D', 'C'), ('D', 'D'), ('D', 'E'), ('E', 'A'), ('E', 'B'), ('E', 'C'), ('E', 'D'), ('E', 'E')]  

 

 

 

 

 

 

 

* ๊ทธ๋ฃนํ™” - itertools.groupby

gen9 = itertools.groupby('AAABBCCCCDDEEE')

groupbyํ•จ์ˆ˜๋Š” ๊ฐ™์€ ์ข…๋ฅ˜๋ผ๋ฆฌ ๋ฌถ์–ด์ค€๋‹ค. 

# print('EX6-12 - ', list(gen9))

 EX6-12 -  [('A', <itertools._grouper object at 0x000001F2EE3E40B8>), ('B', <itertools._grouper object at 0x000001F2EE3E4160>), ('C', <itertools._grouper object at 0x000001F2EE3E4198>), ('D', <itertools._grouper object at 0x000001F2EE3E41D0>), ('E', <itertools._grouper object at 0x000001F2EE3E4208>)] 

for chr, group in gen9:
    print('EX6-12 - ', chr, ' : ', list(group))

 EX6-12 -  A  :  ['A', 'A', 'A'] 
 EX6-12 -  B  :  ['B', 'B'] 
 EX6-12 -  C  :  ['C', 'C', 'C', 'C'] 
 EX6-12 -  D  :  ['D', 'D'] 
 EX6-12 -  E  :  ['E', 'E', 'E'] 

 for๋ฌธ์œผ๋กœ ๋Œ๋ ค์ฃผ๊ณ , group์„ list์— ๋„ฃ์–ด ๊ทธ๋ฃน๋ณ„๋กœ ํ™•์ธํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

 

 

+ Recent posts