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

 

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/35

 

[python ๊ธฐ์ดˆ] 3. ๋ฐ์ดํ„ฐ ํƒ€์ž… (1) (์ž๋ฃŒํ˜•) - list, tuple

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ) <์ด์ „ ๊ธ€> https://silvercoding.tistory.com/34 https://silvercoding.tistory.com/33 https://www.python-course.eu/python3_formatted_output.php Python Tutori..

silvercoding.tistory.com

 

1. ๋”•์…”๋„ˆ๋ฆฌ (Dictionary) 

: ์ˆœ์„œ X, ์ค‘๋ณต X, ์ˆ˜์ • O, ์‚ญ์ œ O 

: Key, Value ํ˜•์‹์œผ๋กœ ๋˜์–ด ์žˆ์Œ. (Json/ MongoDB) 

 

- ์„ ์–ธ 

a = {'name': 'kIM', 'Phone': '010-7777-7777', 'birth': 990101}
b = {0: 'Hello Python', 1: 'Hello Coding'}
c = {'arr': [1, 2, 3, 4, 5]}
print(type(a))

 <class 'dict'> 

๋”•์…”๋„ˆ๋ฆฌ ๊ฐ์ฒด๋ฅผ ์„ ์–ธํ•ด ์ค€๋‹ค. 

 

 

- ์ถœ๋ ฅ 

print(a['name'])
print(a.get('name'))
print(a.get('adress'))  # ์•ˆ์ „ํ•œ ์ฝ”๋”ฉ ๋ฐฉ์‹ : ์ด๊ฑฐ ์“ฐ๋Š” ๊ฒƒ์„ ๊ถŒ์žฅ. ์—๋Ÿฌ๊ฐ€ ์•ˆ๋‚˜๊ธฐ ๋•Œ๋ฌธ 
print(c['arr'][1:3])

 kIM 
 kIM 
 None 
 [2, 3] 

๋”•์…”๋„ˆ๋ฆฌ์—์„œ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•œ 2๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. 1. ํ‚ค์— ์ง์ ‘ ์ ‘๊ทผ , 2. get() ์‚ฌ์šฉ 

1๋ฒˆ ๋ฐฉ๋ฒ•์ด ํŽธ๋ฆฌํ•˜์ง€๋งŒ, ์—†๋Š” ํ‚ค ๊ฐ’์„ ์‚ฌ์šฉํ•˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๊ฒŒ ๋œ๋‹ค. 2๋ฒˆ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด ์—†๋Š” ํ‚ค ๊ฐ’์„ ๋„ฃ๋”๋ผ๋„ None์„ ๋ฐ˜ํ™˜ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ๋” ์•ˆ์ „ํ•œ ์ฝ”๋”ฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

 

- ๋”•์…”๋„ˆ๋ฆฌ ์ถ”๊ฐ€ 

a['adress'] = 'Seoul'
print(a)
a['rank'] = [1, 3, 4]
a['rank2'] = (1, 2, 3)
print(a)

 {'name': 'kIM', 'Phone': '010-7777-7777', 'birth': 990101, 'adress': 'Seoul'} 
 {'name': 'kIM', 'Phone': '010-7777-7777', 'birth': 990101, 'adress': 'Seoul', 'rank': [1, 3, 4], 'rank2': (1, 2, 3)} 

key์™€ value๋ฅผ ์ง์ ‘ ์ ์–ด์ฃผ๋ฉด ๋œ๋‹ค. ๋ฆฌ์ŠคํŠธ์™€ ํŠœํ”Œ ๋˜ํ•œ ์ถ”๊ฐ€๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

 

- keys, values, items 

# print(a.keys()[0]) # ๋ฆฌ์ŠคํŠธ ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผ ๋ถˆ๊ฐ€ 
temp = list(a.keys()) # ---> ํ˜•๋ณ€ํ™˜ ํ•ด์ฃผ์–ด์•ผ ํ•จ 
print(temp)
print(temp[1:3])

 ['name', 'Phone', 'birth', 'adress', 'rank', 'rank2'] 
 ['Phone', 'birth'] 

a.keys() ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋”•์…”๋„ˆ๋ฆฌ a์˜ key๊ฐ’๋“ค๋งŒ ๋ชจ์•„์„œ ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค. ์–ธ๋œป ๋ณด๋ฉด ๋ฆฌ์ŠคํŠธ์ฒ˜๋Ÿผ ์ƒ๊ฒผ์ง€๋งŒ ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ์ฒ˜๋Ÿผ ์ธ๋ฑ์‹ฑ์ด๋‚˜ ์Šฌ๋ผ์ด์‹ฑ์„ ํ•  ์ˆ˜ ์—†๋‹ค. ๋”ฐ๋ผ์„œ list()๋ฅผ ์ด์šฉํ•˜์—ฌ ํ˜•๋ณ€ํ™˜์„ ํ•ด์ฃผ๋ฉด ํ‚ค ๊ฐ’๋“ค์ด ๋‹ด๊ธด ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. 

print(a.values())
print(list(a.values()))

print(a.items())
print(list(a.items()))

 dict_values(['kIM', '010-7777-7777', 990101, 'Seoul', [1, 3, 4], (1, 2, 3)]) 
 ['kIM', '010-7777-7777', 990101, 'Seoul', [1, 3, 4], (1, 2, 3)] 

 dict_items([('name', 'kIM'), ('Phone', '010-7777- 7777'), ('birth', 990101), ('adress', 'Seoul'), ('rank', [1, 3, 4]), ('rank2', (1, 2, 3))]) 
 [('name', 'kIM'), ('Phone', '010-7777-7777'), ('birth', 990101), ('adress', 'Seoul'), ('rank', [1, 3, 4]), ('rank2', (1, 2, 3))] 

values()์™€ items()๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋‹ค! 

 

 

- ํ‚ค ์กด์žฌ ์—ฌ๋ถ€ 

print(1 in b)
print(2 in b)
print('name' in a)

 True 
 False 
 True 

์œ„์™€ ๊ฐ™์ด ๋”•์…”๋„ˆ๋ฆฌ์—์„œ in์„ ์ด์šฉํ•˜๋ฉด key๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ์—ฌ๋ถ€๋ฅผ boolean ํƒ€์ž…์œผ๋กœ ๋ฐ˜ํ™˜ํ•ด ์ค€๋‹ค. 

 

 

 

 

 

 

 

 

 

2. ์ง‘ํ•ฉ (Set) 

: ์ˆœ์„œ X, ์ค‘๋ณต X 

 

- ์„ ์–ธ 

a = set()
b = set([1, 2, 3, 4])
c = set([1, 4, 6, 6, 6])

* ์ฃผ์˜ํ•  ์ ์€ a = { } ์™€ ๊ฐ™์ด ์„ ์–ธํ•˜๋ฉด ๋”•์…”๋„ˆ๋ฆฌ ๊ฐ์ฒด๊ฐ€ ๋งŒ๋“ค์–ด ์ง€๋ฏ€๋กœ ๋นˆ ์ง‘ํ•ฉ์„ ๋งŒ๋“ค ๊ฒฝ์šฐ์—” a = set() ์˜ ํ˜•ํƒœ๋กœ ์„ ์–ธํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค. 

print(type(a))
print(c)

 <class 'set'> 
 {1, 4, 6} 

์œ„์™€ ๊ฐ™์ด ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค

 

 

- ํ˜•๋ณ€ํ™˜ 

: Set ์ž๋ฃŒํ˜•์€ ์ฃผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค. 

t = tuple(b)
print(t)
l = list(b)
print(l)

 (1, 2, 3, 4) 
 [1, 2, 3, 4] 

์ค‘๋ณต ๊ฐ’์ด ๋“ค์–ด๊ฐ€์ง€ ์•Š๋„๋ก set์„ ์ƒ์„ฑํ•œ ํ›„ ํŠœํ”Œ์ด๋‚˜ ๋ฆฌ์ŠคํŠธ ๋“ฑ์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ์ผ์ด ๋งŽ๋‹ค๊ณ  ํ•œ๋‹ค.  

 

 

 

 

* ์ง‘ํ•ฉ ์—ฐ์‚ฐ (๊ต์ง‘ํ•ฉ, ํ•ฉ์ง‘ํ•ฉ, ์ฐจ์ง‘ํ•ฉ) 

s1 = set([1, 2, 3, 4, 5, 6])
s2 = set([4, 5, 6, 7, 8, 9])

์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ์ง‘ํ•ฉ์„ ์„ ์–ธํ•œ๋‹ค. 

 

 

- ๊ต์ง‘ํ•ฉ : intersection / &

print(s1.intersection(s2))
print(s1 & s2)

 {4, 5, 6} 
 {4, 5, 6} 

 

 

- ํ•ฉ์ง‘ํ•ฉ : union / |

print(s1.union(s2))
print(s1 | s2)

 {1, 2, 3, 4, 5, 6, 7, 8, 9} 
 {1, 2, 3, 4, 5, 6, 7, 8, 9} 

 

 

- ์ฐจ์ง‘ํ•ฉ : difference / -

print(s1.difference(s2))
print(s1 - s2)

 {1, 2, 3, 4, 5, 6, 7, 8, 9} 
 {1, 2, 3} 
 {1, 2, 3} 

 

 

 

- ์ถ”๊ฐ€ & ์ œ๊ฑฐ 

s3 = set([7, 8, 10, 15])
s3.add(18)
s3.add(7)

print(s3)

 {7, 8, 10, 15, 18} 

addํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ’์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

s3.remove(15)
print(s3)

 {7, 8, 10, 18} 

removeํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ’ ์ œ๊ฑฐ๊นŒ์ง€ ๊ฐ€๋Šฅํ•˜๋‹ค. 

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

 

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/34

 

[python ๊ธฐ์ดˆ] 2. ๋ฐ์ดํ„ฐ ํƒ€์ž… Data Type - ์ˆซ์žํ˜•, ๋ฌธ์ž์—ด

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ) <์ด์ „ ๊ธ€> https://silvercoding.tistory.com/33 https://www.python-course.eu/python3_formatted_output.php Python Tutorial: Formatted Output Even though it..

silvercoding.tistory.com

 

 

 

 

 

1. ๋ฆฌ์ŠคํŠธ (list) 

: ์ˆœ์„œ O, ์ค‘๋ณต O, ์‚ญ์ œ O 

- ์„ ์–ธ 

a = []
b = list()
c = [1, 2, 3, 4]
d = [10, 100, 'Pen', 'Banana', 'Orange']
e = [10, 100, ['Pen', 'Banana', 'Orange']]

์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฆฌ์ŠคํŠธ๋ฅผ ์„ ์–ธ ํ•ด ์ค„ ์ˆ˜ ์žˆ๋‹ค. 

 

 

- ์ธ๋ฑ์‹ฑ

print(d[3])
print(d[-2])
print(d[0] + d[1])
print(e[2][1])
print(e[-1][-2])

 Banana 
 Banana 
 110 
 Banana 
 Banana 

 

 

 

- ์Šฌ๋ผ์ด์‹ฑ 

print(d[0:3])
print(e[2][1:3])

 [10, 100, 'Pen'] 
 ['Banana', 'Orange'] 

 

 

 

- ์—ฐ์‚ฐ 

print(c + d)
print(c * 3)
print(str(c[0]) + 'hi')

 [1, 2, 3, 4, 10, 100, 'Pen', 'Banana', 'Orange'] 
 [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4] 
 1hi 

 

 

 

- ๋ฆฌ์ŠคํŠธ ์ˆ˜์ •, ์‚ญ์ œ 

c[0] = 77
print(c)

 [77, 2, 3, 4] 

0๋ฒˆ์จฐ ์ธ๋ฑ์Šค์— ๊ฐ’ ๋„ฃ์–ด์ฃผ๊ธฐ 

c[1:2] = [100, 1000, 10000]
print(c) # ์›์†Œ๊ฐ€ ๋“ค์–ด๊ฐ

 [77, 100, 1000, 10000, 3, 4] 

์Šฌ๋ผ์ด์‹ฑ์„ ์ด์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ๊ฐ’์„ ๋„ฃ์–ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

c[1] = ['a', 'b', 'c']
print(c) # ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋“ค์–ด๊ฐ

 [77, ['a', 'b', 'c'], 1000, 10000, 3, 4] 

๋ฆฌ์ŠคํŠธ ์ž์ฒด๋ฅผ ๋„ฃ์–ด์ฃผ๋ ค๋ฉด ์ธ๋ฑ์‹ฑ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. 

del c[1]
print(c)

 [77, 1000, 10000, 3, 4] 

del์„ ์ด์šฉํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ’์„ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค. 

del c[-1]
print(c)

 [77, 1000, 10000, 3] 

 

 

 

 

 

-๋ฆฌ์ŠคํŠธ ๊ด€๋ จ ํ•จ์ˆ˜ 

(1) append 

y = [5, 2, 3, 1, 4]
print(y)
y.append(6) # ๋์— ์ถ”๊ฐ€ 
print(y)

 [5, 2, 3, 1, 4] 
 [5, 2, 3, 1, 4, 6] 

๋ฆฌ์ŠคํŠธ์˜ ๊ฐ€์žฅ ๋์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•ด ์ค€๋‹ค. 

 

(2) sort

y.sort() # ์ •๋ ฌ 
print(y)

 [1, 2, 3, 4, 5, 6] 

๋ฆฌ์ŠคํŠธ๋ฅผ ์ •๋ ฌํ•ด ์ค€๋‹ค. 

 

(3) reverse

y.reverse()
print(y)

 [6, 5, 4, 3, 2, 1] 

๋ฆฌ์ŠคํŠธ๋ฅผ ๋’ค์ง‘์–ด ์ค€๋‹ค. 

 

(4) insert

y.insert(2, 7)
print(y)

 [6, 5, 7, 4, 3, 2, 1] 

append์™€๋Š” ๋‹ค๋ฅด๊ฒŒ ์›ํ•˜๋Š” ์ธ๋ฑ์Šค์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค. 

 

(5) remove

y.remove(2)
y.remove(7)
print(y) # ์›์†Œ ๊ฐ’์„ ์ง€์šด๋‹ค.

 [6, 5, 4, 3, 1] 

del์€ ์ธ๋ฑ์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ ์ง€์›Œ์ฃผ์—ˆ๋Š”๋ฐ, removeํ•จ์ˆ˜๋Š” ๊ฐ’์„ ๋„ฃ์œผ๋ฉด ๊ทธ ๊ฐ’์„ ์ง€์›Œ์ค€๋‹ค. 

 

(7) pop

y.pop()
print(y) # LIFO ์Šคํƒ ์ž๋ฃŒ๊ตฌ์กฐ

 [6, 5, 4, 3]  

pop์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ€์žฅ ๋์— ์žˆ๋Š” ๊ฐ’์„ ์ง€์›Œ์ค€๋‹ค. LIFO(Last In First Out) ์˜ ์ž๋ฃŒ๊ตฌ์กฐ๋กœ ์‚ฌ์šฉ๋œ๋‹ค. 

 

(8) extend

ex = [88, 77]
# y.append(ex) # ๋ฆฌ์ŠคํŠธ ์ž์ฒด๋ฅผ ์ถ”๊ฐ€
y.extend(ex) # ๊ฐ’์„ ์ถ”๊ฐ€ 
print(y)

 [6, 5, 4, 3, 88, 77] 

๋ฆฌ์ŠคํŠธ ์•ˆ์— ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ๋ฆฌ์ŠคํŠธ ์•ˆ์— ์—ฌ๋Ÿฌ ๊ฐ’์„ ํ•œ ๋ฒˆ์— ๋„ฃ์–ด์ฃผ๊ณ  ์‹ถ์„ ๋•Œ extendํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. 

 

 

 

* ๋ฆฌ์ŠคํŠธ ์‚ญ์ œ ์ด์ •๋ฆฌ 

del, remove, pop ( : ์˜ˆ์™ธ ๋ฐœ์ƒ ์ฃผ์˜) 

 

 

 

 

 

 

2. ํŠœํ”Œ (tuple) 

: ์ˆœ์„œ O, ์ค‘๋ณต O, ์ˆ˜์ • X, ์‚ญ์ œ X 

- ์„ ์–ธ 

a = ()
b = (1,)
c = (1, 2, 3, 4)
d = (10, 100, ('a', 'b', 'c'))

์ด์™€ ๊ฐ™์ด ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•์œผ๋กœ ํŠœํ”Œ์„ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

- ์‚ญ์ œ : ๋ถˆ๊ฐ€ 

del c[2] # -> ์‚ญ์ œ ๋ถˆ๊ฐ€

 TypeError: 'tuple' object doesn't support item deletion 

ํŠœํ”Œ์€ ์ˆ˜์ •์ด๋‚˜ ์‚ญ์ œ๋ฅผ ํ•  ์ˆ˜ ์—†๋‹ค. 

 

 

- ์ธ๋ฑ์‹ฑ 

print(c[2])
print(c[3])
print(d[2][2])

 3 
 4 
 c 

 

 

- ์Šฌ๋ผ์ด์‹ฑ 

print(d[2:])
print(d[2][0:2])

 (('a', 'b', 'c'),) 
 ('a', 'b') 

 

 

- ์—ฐ์‚ฐ 

print(c + d)
print(c * 3)

 (1, 2, 3, 4, 10, 100, ('a', 'b', 'c')) 
 (1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4) 

 

 

- ํŠœํ”Œ ๊ด€๋ จ ํ•จ์ˆ˜ 

z = (5, 2, 1, 3, 1)
print(z)
print(3 in z)
print(z.index(5))  # ์ธ๋ฑ์Šค ๊ฐ’์„ ๋ฐ˜ํ™˜ 
print(z.count(1))  # 1 ์ด ๋ช‡๊ฐœ๊ฐ€ ์žˆ๋ƒ

 (5, 2, 1, 3, 1) 
 True 
 0 
 2 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

 

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/33

 

[python ๊ธฐ์ดˆ] 1. print ํ•จ์ˆ˜

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ) <์ฐธ๊ณ  ๋งํฌ> https://www.python-course.eu/python3_formatted_output.php Python Tutorial: Formatted Output Even though it may look so, the formatting is not..

silvercoding.tistory.com

 

* ๋ฐ์ดํ„ฐ ํƒ€์ž… ํ•œ๋ˆˆ์— ๋ณด๊ธฐ 

v_str = "Niceman"
v_bool = True
v_str2 = "Goodboy"
v_float = 10.3
v_int = 7
v_dict = {
    "name" : "Kim", 
    "age" : 25
}
v_list = [3, 5, 7]
v_tuple = 3, 5, 7
v_set = {7, 8, 9}
print(type(v_str))
print(type(v_bool))
print(type(v_str2))
print(type(v_float))
print(type(v_int))
print(type(v_dict))
print(type(v_list))
print(type(v_tuple))
print(type(v_set))

 <class 'str'> 
 <class 'bool'> 
 <class 'str'>  
 <class 'float'> 
 <class 'int'> 
 <class 'dict'> 
 <class 'list'> 
 <class 'tuple'> 
 <class 'set'> 

 

 

 

 

 

1. ์ˆซ์žํ˜• 

<์ˆซ์žํ˜• ์—ฐ์‚ฐ์ž ์ฐธ๊ณ > 

 https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex

 

Built-in Types — Python 3.9.6 documentation

The following sections describe the standard types that are built into the interpreter. The principal built-in types are numerics, sequences, mappings, classes, instances and exceptions. Some collection classes are mutable. The methods that add, subtract,

docs.python.org

i1 = 39 
i2 = 939 
big_int1 = 999999999999999999999999999999999999999
big_int2 = 777777777777777777777777777777777777777
f1 = 1.234 
f2 = 3.1415
f3 = .5
f4 = 10.
print(i1 * i2)
print(big_int1 * big_int2)
print(f1 ** f2)
print(f3 + i2)

 36621 
 777777777777777777777777777777777777776222222222222222222222222222222222222223       
 1.93582713947248 
 939.5 

 

result = f3 + i2 
print(result, type(result))

 939.5 <class 'float'> 

int + float๋ฅผ ํ•˜๋ฉด ์ž๋™์ ์œผ๋กœ float ํƒ€์ž…์ด ๋œ๋‹ค. 

 

 

a = 5. 
b = 4
c = 10
print(type(a), type(b))
result2 = a + b 
print(result2)

 <class 'float'> <class 'int'> 
 9.0 

 

 

- ์ˆซ์žํ˜• ํ˜•๋ณ€ํ™˜ (์œ ์—ฐํ•œ ํ˜•๋ณ€ํ™˜ ๊ฐ€๋Šฅ) 

* int, float, complex(๋ณต์†Œ์ˆ˜ : ์ž˜ ์•ˆ์”€) 

print(int(result2))
print(float(c))
print(complex(3))
print(int(True))
print(int(False))
print(int('3'))
print(complex(False))

 9 
 10.0 
 (3+0j) 
 1 
 0 

True, False๋Š” ๊ฐ๊ฐ ์ •์ˆ˜ํ˜•์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋ฉด 1, 0 ์ด ๋œ๋‹ค. 
 3 
 0j 

์ด์ฒ˜๋Ÿผ ํŒŒ์ด์ฌ์€ ์œ ์—ฐํ•œ ํ˜•๋ณ€ํ™˜์„ ์ง€์›ํ•œ๋‹ค .

y = 100 
y += 100 
print(y)

 200 

 

 

 

 

<์ˆ˜์น˜ ์—ฐ์‚ฐ ํ•จ์ˆ˜ ์ฐธ๊ณ > 

https://docs.python.org/3/library/math.html

 

math — Mathematical functions — Python 3.9.6 documentation

math — Mathematical functions This module provides access to the mathematical functions defined by the C standard. These functions cannot be used with complex numbers; use the functions of the same name from the cmath module if you require support for co

docs.python.org

print(abs(-7))
n, m = divmod(100, 8)  # ๋ชซ, ๋‚˜๋จธ์ง€ 
print(n, m)

 7 

abs() ํ•จ์ˆ˜๋Š” ์ ˆ๋Œ“๊ฐ’์„ ์”Œ์›Œ์ฃผ๋Š” ํ•จ์ˆ˜์ด๋‹ค. 
 12 4 

divmod()ํ•จ์ˆ˜๋Š” ๋ชซ๊ณผ ๋‚˜๋จธ์ง€๋ฅผ ๋™์‹œ์— ๋ฐ˜ํ™˜ํ•ด ์ค€๋‹ค. 

 

import math
print(math.ceil(5.1))  # ์ด ์ˆซ์ž๋ณด๋‹ค ํฌ๋ฉด์„œ ๊ฐ€์žฅ ์ž‘์€ ์ •์ˆ˜ 
print(math.floor(3.874))  # ์ด ์ˆซ์ž๋ณด๋‹ค ์ž‘์œผ๋ฉด์„œ ๊ฐ€์žฅ ํฐ ์ •์ˆ˜

 6 
 3 

math.ceil() : ์ธ์ž์— ๋“ค์–ด๊ฐ„ ์ˆซ์ž๋ณด๋‹ค ํฌ๋ฉด์„œ ๊ฐ€์žฅ ์ž‘์€ ์ •์ˆ˜ 

math.floor : ์ธ์ž์— ๋“ค์–ด๊ฐ„ ์ˆซ์ž๋ณด๋‹ค ์ž‘์œผ๋ฉด์„œ ๊ฐ€์žฅ ํฐ ์ •์ˆ˜ 

 

 

 

 

 

2. ๋ฌธ์ž์—ด 

str1 = "I am a geak!"
str2 = "NiceGirl"
str3 = ""
str4 = str()
print(len(str1), len(str2), len(str3), len(str4))  # ๊ณต๋ฐฑ ํฌํ•จ

 12 8 0 0 

๊ณต๋ฐฑ์„ ํฌํ•จํ•˜์—ฌ ๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” len() ํ•จ์ˆ˜

 

 

escape_str1 = "Do you have a \"big collection\"?"
print(escape_str1)
escape_str2 = "Tab\tTab\tTab"
print(escape_str2)

 Do you have a "big collection"? 

๊ฐ™์€ ์ข…๋ฅ˜ ๋”ฐ์˜ดํ‘œ๋ฅผ ์•ˆ์— ๋„ฃ๊ธฐ ์œ„ํ•˜์—ฌ ์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. 

 Tab     Tab     Tab 

 

 

 

 

- Raw String

raw_s1 = r'C:\Programs\Test\Bin'
print(raw_s1)
raw_s2 = r"\\a\\a"
print(raw_s2)

 C:\Programs\Test\Bin 
 \\a\\a 

rwa string์œผ๋กœ ์ด์Šค์ผ€์ดํ”„๋ฌธ ์—†์ด ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

 

 

 

 

- ๋ฉ€ํ‹ฐ๋ผ์ธ : ๋‹ค์Œ์ค„๊ณผ ์ด์–ด์ง„๋‹ค. 

multi = \
"""
๋ฌธ์ž์—ด 
๋ฉ€ํ‹ฐ๋ผ์ธ 
ํ…Œ์ŠคํŠธ
"""
print(multi)

 ๋ฌธ์ž์—ด 
 ๋ฉ€ํ‹ฐ๋ผ์ธ 
 ํ…Œ์ŠคํŠธ 

 

 

 

 

 

- ๋ฌธ์ž์—ด ์—ฐ์‚ฐ 

str_o1 = '*'
str_o2 = 'abc'
str_o3 = 'def'
str_o4 = 'Niceman'
print(str_o1 * 100)
print(str_o2 + str_o3)
print(str_o1 * 3)
print('a' in str_o4)
print('f' in str_o4)
print('z' not in str_o4)

 **************************************************************************************************** 
 abcdef 
 *** 
 True 
 False 
 True 

๋ฌธ์ž์—ด์— ์ˆซ์ž๋ฅผ ๊ณฑํ•˜๊ฑฐ๋‚˜, ๋ฌธ์ž์—ด๋ผ๋ฆฌ ๋”ํ•˜๋Š” ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

 

 

 

 

- ๋ฌธ์ž์—ด ํ˜•๋ณ€ํ™˜ 

print(str(77) + 'a')
print(str(10.4))

 77a 
 10.4 

 

 

 

 

 

 

 

- ๋ฌธ์ž์—ด ํ•จ์ˆ˜

<๋ฌธ์ž์—ด ํ•จ์ˆ˜ ์ฐธ๊ณ > 

https://www.w3schools.com/python/python_ref_string.asp

 

Python String Methods

Python String Methods Python has a set of built-in methods that you can use on strings. Note: All string methods returns new values. They do not change the original string. Method Description capitalize()Converts the first character to upper case casefold(

www.w3schools.com

 

a = 'niceman'
b = 'orange'
print(a.islower())  # ์†Œ๋ฌธ์ž์ธ์ง€ : Bool ๋ฐ˜ํ™˜
print(b.endswith('e'))  # e๋กœ ๋๋‚˜๋Š”์ง€ : Bool ๋ฐ˜ํ™˜
print(a.capitalize())  # ์ฒซ ๊ธ€์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ 
print(a.replace('nice', 'good'))  # nice๋ฅผ good์œผ๋กœ ๋Œ€์ฒดํ•ด๋ผ
print(list(reversed(b)))  # ๋’ค์ง‘์–ด์„œ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ˜ํ™˜ํ•˜๋ผ

 True 

 True 
 Niceman 
 goodman 
 ['e', 'g', 'n', 'a', 'r', 'o'] 

 

 

 

 

 

 

 

- ๋ฌธ์ž์—ด ์Šฌ๋ผ์ด์‹ฑ 

a = 'niceman'
b = 'orange'
print(a[0:3])
print(a[0:4])
print(a[0:len(a)])
print(a[:4])
print(b[0:4:2])
print(b[1:-2])
print(b[::-1])

 nic 
 nice 
 niceman 
 nice 
 oa 
 ran 
 egnaro 

 a[0:n] ์ผ ๋•Œ 0๋ฒˆ์งธ ๊ธ€์ž๋ถ€ํ„ฐ n-1๋ฒˆ์งธ ๊ธ€์ž๊นŒ์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 

 

 

 

 

 

 

 

 

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

 

 

<์ฐธ๊ณ  ๋งํฌ>

https://www.python-course.eu/python3_formatted_output.php

 

Python Tutorial: Formatted Output

Even though it may look so, the formatting is not part of the print function. If you have a closer look at our examples, you will see that we passed a formatted string to the print function. Or to put it in other words: If the string modulo operator is app

www.python-course.eu

 

 

 

 

1. ๊ธฐ๋ณธ ์ถœ๋ ฅ 

print('Hello Python!')
print("Hello Python!")
print("""Hello Python!""")
print('''Hello Python!''')

 Hello Python! 
 Hello Python! 
 Hello Python! 
 Hello Python! 

์ž‘์€๋”ฐ์˜ดํ‘œ, ํฐ๋”ฐ์˜ดํ‘œ 1๊ฐœ ํ˜น์€ 3๊ฐœ๋กœ ๊ฐ์‹ธ์ค€๋‹ค. 

 

 

 

 

 

 

 

2. seperator ์˜ต์…˜ ์‚ฌ์šฉ 

print('T', 'E', 'S', 'T', sep='')
print('2019', '02', '19', sep='-')
print('niceman', 'google.com', sep="@")

 TEST 
 2019-02-19 
 niceman@google.com 

printํ•จ์ˆ˜์˜ ์ธ์ž์— sep= ์˜ต์…˜์„ ๋„ฃ์–ด์ฃผ๋ฉด sep์— ๋“ค์–ด๊ฐ€๋Š” ๋ฌธ์ž๊ฐ€ ์ค‘๊ฐ„์— ๋“ค์–ด ๊ฐ€ ๋ฌธ์ž์—ด์„ ์ด์–ด ์ค€๋‹ค. 

 

 

 

 

 

 

 

3. end ์˜ต์…˜ ์‚ฌ์šฉ 

print('Welcom To', end=' ')
print('the black prade', end=' ')
print('piano notes')

 Welcom To the black prade piano notes 

end์˜ต์…˜์€ defalut๊ฐ€ \n์œผ๋กœ ๋“ค์–ด ๊ฐ€ ์žˆ์–ด ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ์ž๋™์œผ๋กœ ์ค„๋ฐ”๊ฟˆ์ด ๋์—ˆ๋‹ค. end=' ' ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์œ„์™€ ๊ฐ™์ด ๊ทธ ๋‹ค์Œ ์ค„์ด ํ˜ธ์ถœ๋  ๋•Œ ๋„์–ด ์“ฐ๊ธฐ ํ•œ ๋ฒˆ ํ›„ ์ถœ๋ ฅ์ด ๋œ๋‹ค. 

 

 

 

 

 

 

 

4. format ์‚ฌ์šฉ  { }. ( ) 

print('{} and {}'.format('You', 'Me'))
print("{0} and {1} and {0}".format('You', 'Me'))
print('{a} and {b}'.format(a='You', b='Me'))

 You and Me 
 You and Me and You 
 You and Me 

format ํ•จ์ˆ˜๋กœ ๋ฌธ์ž์—ด์— ๊ฐ’์„ ๋„ฃ์–ด์ค„ ์ˆ˜ ์žˆ๋‹ค. 

 

print("%s favorite number is %d" % ('Silver', 7))
print("Test1: %5d, Price: %4.2f" % (776, 6534.123))

 Silver favorite number is 7 
 Test1:   776, Price: 6534.12 

* %s : ๋ฌธ์ž, %d : ์ •์ˆ˜, %f : ์‹ค์ˆ˜

%๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ธ€์ž์ˆ˜๋‚˜, type์— ๋Œ€ํ•ด ๋ช…์‹œ์ ์œผ๋กœ ๊ธฐ์ž…ํ•ด ๋†“์„ ์ˆ˜ ์žˆ๋‹ค. 

 

print("Test1: {0: 5d}, Price: {1: 4.2f}".format(776, 6534.123))
print('Test1: {a: 5d}, Price: {b: 4.2f}'.format(a=776, b=6534.123))

 Test1:   776, Price:  6534.12 
 Test1:   776, Price:  6534.12 

์œ„์™€ ๊ฐ™์ด format์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ธ€์ž์ˆ˜์™€ type์„ ์ง€์ •ํ•ด์ค„ ์ˆ˜๋„ ์žˆ๋‹ค. 

 

 

 

 

 

 

 

5. ์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž 

print("'you'")
print('\'you\'')
print('"you"')
print("""'you'""")
print('\\you\\\n')
print('\t\t\ttest')

 'you' 

์ข…๋ฅ˜๊ฐ€ ๋‹ค๋ฅธ ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ด์ฒ˜๋Ÿผ ์•ˆ์— ์žˆ๋Š” ๋”ฐ์˜ดํ‘œ๋กœ ๋‚˜์˜ค๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 'you' 

์ข…๋ฅ˜๊ฐ€ ๊ฐ™์€ ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ถœ๋ ฅํ•œ๋‹ค. 

 "you" 
 'you' 

 \you\ 

                              
                        test 

\\, \n, \t ์„ ์‚ฌ์šฉํ•˜์—ฌ \, ์ค„๋ฐ”๊ฟˆ, ํƒญ์„ ์‹คํ–‰ํ•  ์ˆ˜๋„ ์žˆ๋”ฐ. 

 

 

* ์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž 

\n : ๊ฐœํ–‰

\t : ํƒญ

\\ : ๋ฌธ์ž

\' : ๋ฌธ์ž

\" : ๋ฌธ์ž

\r : ์บ๋ฆฌ์ง€ ๋ฆฌํ„ด

\f : ํผ ํ”ผ๋“œ

\a : ๋ฒจ ์†Œ๋ฆฌ

\b : ๋ฐฑ ์ŠคํŽ˜์ด์Šค

\000 : ๋„ ๋ฌธ์ž

...

 

 

 

 

 

 

 

 

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

 

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/31

 

[python ์‹ฌํ™”] 14. Future ๋™์‹œ์„ฑ/ ๋น„๋™๊ธฐ ์ž‘์—… ์‹คํ–‰/ ThreadPoolExecutor, ProcessPoolExecutor

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ) <์ด์ „ ๊ธ€> https://silvercoding.tistory.com/30 https://silvercoding.tistory.com/29 https://silvercoding.tistory.com/28 https://silvercoding.tistory.com/27..

silvercoding.tistory.com

 

 

< Asyncio ์ฐธ๊ณ ๋ฌธ์„œ >

https://docs.python.org/ko/3/library/asyncio.html

 

asyncio — ๋น„๋™๊ธฐ I/O — Python 3.9.6 ๋ฌธ์„œ

 

docs.python.org

 

- Asyncio 

: ๋น„๋™๊ธฐ I/O Coroutine ์ž‘์—… ( *๋™๊ธฐ: ๊ธฐ๋‹ค๋ ค์•ผ ํ•˜๋Š” ๊ฒƒ , ๋น„๋™๊ธฐ: ๊ธฐ๋‹ค๋ฆฌ์ง€ ์•Š๊ณ  ๋ฐ”๋กœ ์‹คํ–‰๋˜๋Š” ๊ฒƒ) 

: Generator : ๋ฐ˜๋ณต์ ์ธ ๊ฐ์ฒด Return (yield) , ์ฆ‰, ์‹คํ–‰ํ•˜๊ณ  stop -> ๋‹ค๋ฅธ ์ž‘์—…์œผ๋กœ์˜ ์œ„์ž„ -> stop ์ง€์ ๋ถ€ํ„ฐ ์žฌ ์‹คํ–‰ํ•˜๋Š” ์›๋ฆฌ์ด๋‹ค. 

: Non-Blocking ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ์— ์ ํ•ฉํ•˜๋‹ค. 

 

 

* ๋ณธ ํฌ์ŠคํŒ…์—์„œ ์‹ค์Šตํ•  ๋ฐฉ๋ฒ•๋“ค 

1. ์ˆœ์ฐจ์‹คํ–‰ 

2. ์“ฐ๋ ˆ๋“œ๋งŒ ์‚ฌ์šฉ 

3. ์ฝ”๋ฃจํ‹ด์—์„œ ์“ฐ๋ ˆ๋“œ ์šฐํšŒ ์‚ฌ์šฉ 

 

 

 

- 1. BlockIO ์˜ˆ์ œ  : ์ˆœ์ฐจ์‹คํ–‰ 

import timeit
from urllib.request import urlopen

ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ import ํ•˜๊ธฐ 

 

urls = ['http://daum.net', 'https://google.com', 'https://apple.com', 'https://tistory.com', 'https://github.com', 'https://naver.com']

์˜ˆ์ œ์—์„œ url์„ openํ•ด ๋ณด๋Š” ์‹ค์Šต์„ ํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— url ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด ์ค€๋‹ค. ์ด 6 ๊ฐœ์˜ url์„ ๋„ฃ์–ด ์ฃผ์—ˆ๋‹ค. 

 

start = timeit.default_timer()

์ˆœ์ฐจ ์‹คํ–‰์„ ํ•˜๊ธฐ ์ „ ์‹œ๊ฐ„์„ ๊ธฐ๋กํ•ด ๋‘”๋‹ค. ์ด๋ฒˆ์—๋Š” timeit.default_timer()๋ฅผ ์ด์šฉํ•˜์—ฌ ์‹œ๊ฐ„์„ ์ธก์ •ํ•œ๋‹ค. 

 

 

* ์ˆœ์ฐจ ์‹คํ–‰๋ถ€ 

for url in urls:
    print('Start', url)
    # ์‹ค์ œ ์š”์ฒญ 
    text = urlopen(url)
    # ์‹ค์ œ ๋‚ด์šฉ
    # print('Contents', text.read())
    print('Done', url)

์œ„์™€ ๊ฐ™์ด urlopen์„ ์‚ฌ์šฉํ•˜์—ฌ url์— ์‹ค์ œ ์š”์ฒญ์„ ํ•œ๋‹ค. 

 

 

* ์‹œ๊ฐ„ ์ธก์ • : ์™„๋ฃŒ์‹œ๊ฐ„ - ์‹œ์ž‘์‹œ๊ฐ„ / ๊ฒฐ๊ณผ ์ถœ๋ ฅ

duration = timeit.default_timer() - start

# ์ด ์‹คํ–‰์‹œ๊ฐ„ 
print('Total Time', duration)

 Start http://daum.net 
 Done http://daum.net 
 Start https://google.com 
 Done https://google.com 
 Start https://apple.com 
 Done https://apple.com 
 Start https://tistory.com 
 Done https://tistory.com 
 Start https://github.com 
 Done https://github.com 
 Start https://naver.com 
 Done https://naver.com 
 Total Time 12.8696096 

์ˆœ์ฐจ์ ์œผ๋กœ url๋ฆฌ์ŠคํŠธ์— ์ ์—ˆ๋˜ ์ˆœ์„œ๋Œ€๋กœ url์š”์ฒญ์ด ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์‹œ๊ฐ„์€ ์ด 13์ดˆ ์ •๋„๊ฐ€ ๊ฑธ๋ ธ๋‹ค. 

 

 

 

 

 

 

 

 

 

 

*** BlockIO : Thread ์‚ฌ์šฉ 

: ์“ฐ๋ ˆ๋“œ ๊ฐœ์ˆ˜ ๋ฐ GIL ๋ฌธ์ œ ์—ผ๋‘, ๊ณต์œ  ๋ฉ”๋ชจ๋ฆฌ ๋ฌธ์ œ ํ•ด๊ฒฐ (๋ฎคํ…์Šค)

 

 

- 2. Thread๋งŒ ์‚ฌ์šฉ 

 

* ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ import 

import timeit
from urllib.request import urlopen
from concurrent.futures import ThreadPoolExecutor
import threading

 

* url ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ ๋ฐ ์‹œ๊ฐ„ ๊ธฐ๋ก 

urls = ['http://daum.net', 'https://google.com', 'https://apple.com', 'https://tistory.com', 'https://github.com', 'https://naver.com']
start = timeit.default_timer()

 

 

* fetch ํ•จ์ˆ˜ ์„ ์–ธ

def fetch(url):
    print('Thread Name : ', threading.current_thread().getName(), 'Start', url)  # ํ˜„์žฌ ์“ฐ๋ž˜๋“œ๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์•Œ๋ ค์คŒ
    urlopen(url)
    print('Thread Name : ', threading.current_thread().getName(), 'Done', url)

ํ˜„์žฌ ์“ฐ๋ ˆ๋“œ์˜ ์ด๋ฆ„๊ณผ , ์‹œ์ž‘(start) ๊ณผ ์™„๋ฃŒ(Done)์˜ ๊ณผ์ •์„ ์ถœ๋ ฅํ•˜๋„๋ก ํ•˜๋Š” fetch ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. 

 

 

* main ํ•จ์ˆ˜ ์„ ์–ธ - fetchํ•จ์ˆ˜ ์‚ฌ์šฉ 

def main():
    with ThreadPoolExecutor(max_workers=10) as executor: 
        for url in urls:
            executor.submit(fetch, url)

ThreadPoolExecutor๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , max_workers๋Š” 10์œผ๋กœ ๋ฐ›์•„์ค€๋‹ค. ์ด์ œ for๋ฌธ์„ ์ด์šฉํ•˜์—ฌ url์„ ํ•œ๊ฐœ์”ฉ ๋„ฃ์–ด์„œ submit์œผ๋กœ fetchํ•จ์ˆ˜์— ์ ์šฉํ•ด ์ค€๋‹ค. 

 

 

 

* ์‹คํ–‰ 

if __name__ == '__main__':
    # ํ•จ์ˆ˜ ์‹คํ–‰ 
    main()
    
    # ์™„๋ฃŒ์‹œ๊ฐ„ - ์‹œ์ž‘์‹œ๊ฐ„
    duration = timeit.default_timer() - start

    # ์ด ์‹คํ–‰์‹œ๊ฐ„   
    print('Total Time', duration)

 Thread Name :  ThreadPoolExecutor-0_0 Start http://daum.net 
 Thread Name :  ThreadPoolExecutor-0_1 Start https://google.com 
 Thread Name :  ThreadPoolExecutor-0_2 Start https://apple.com 
 Thread Name :  ThreadPoolExecutor-0_3 Start https://tistory.com 
 Thread Name :  ThreadPoolExecutor-0_4 Start https://github.com 
 Thread Name :  ThreadPoolExecutor-0_5 Start https://naver.com 
 Thread Name :  ThreadPoolExecutor-0_4 Done https://github.com 
 Thread Name :  ThreadPoolExecutor-0_2 Done https://apple.com 
 Thread Name :  ThreadPoolExecutor-0_5 Done https://naver.com 
 Thread Name :  ThreadPoolExecutor-0_3 Done https://tistory.com 
 Thread Name :  ThreadPoolExecutor-0_0 Done http://daum.net 
 Thread Name :  ThreadPoolExecutor-0_1 Done https://google.com 
 Total Time 0.9581279
 

start๋Š” for๋ฌธ์— ๋“ค์–ด๊ฐ„ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅ์ด ๋˜์—ˆ๋Š”๋ฐ, Done์€ ์ˆœ์„œ๊ฐ€ ์ผ์ •ํ•˜์ง€ ์•Š์€ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. submit ํ•จ์ˆ˜๋Š” ๋จผ์ € ์ผ์ด ๋๋‚˜๋Š” ์ˆœ์„œ๋Œ€๋กœ ์˜ค๋Š” ๊ฒƒ์ผ ๋ฟ, ์ˆœ์„œ์— ๋Œ€ํ•œ ์‹ ๊ฒฝ์„ ์“ฐ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.  ์‹œ๊ฐ„์€ 1์ดˆ๋„ ๋˜์ง€ ์•Š๋Š”๋‹ค! ๊ต‰์žฅํžˆ ์ค„์–ด ๋“  ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

 

 

 

 

 

- 3. ์ฝ”๋ฃจํ‹ด์—์„œ ์“ฐ๋ ˆ๋“œ ์šฐํšŒ ์‚ฌ์šฉ 

 

<์ฐธ๊ณ >

https://docs.python.org/ko/3/library/asyncio-task.html

 

์ฝ”๋ฃจํ‹ด๊ณผ ํƒœ์Šคํฌ — Python 3.9.6 ๋ฌธ์„œ

์ฝ”๋ฃจํ‹ด๊ณผ ํƒœ์Šคํฌ ์ด ์ ˆ์—์„œ๋Š” ์ฝ”๋ฃจํ‹ด๊ณผ ํƒœ์Šคํฌ๋กœ ์ž‘์—…ํ•˜๊ธฐ ์œ„ํ•œ ๊ณ ๊ธ‰ asyncio API์— ๊ด€ํ•ด ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. async/await ๋ฌธ๋ฒ•์œผ๋กœ ์„ ์–ธ๋œ ์ฝ”๋ฃจํ‹ด์€ asyncio ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋Š” ๊ธฐ๋ณธ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ

docs.python.org

*** ์ฝ”๋ฃจํ‹ด์€ ๋‹จ์ผ ์“ฐ๋ ˆ๋“œ๋ผ๋Š” ๊ฒƒ์ด ํ•ต์‹ฌ์ด๋‹ค. ํ•จ์ˆ˜ ์—ฌ๋Ÿฌ๊ฐœ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๋ฉ€ํ‹ฐ ์“ฐ๋ ˆ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋“ฏ ๋ณด์ด๊ฒŒ ํ•ด์ค€๋‹ค. ๋‹จ์ผ ์“ฐ๋ ˆ๋“œ์—์„œ ์ตœ์ ์˜ ์„ฑ๋Šฅ์„ ์ด๋Œ์–ด๋‚ด๊ธฐ ์œ„ํ•ด ์ œ์–ด๋ฅผ ์–‘๋ณดํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋ฉฐ, ๋น ๋ฅด๊ฒŒ ๋™์‹œ์„ฑ ๊ธฐ๋ฒ•์„ ํ™œ์šฉํ•˜๋Š” ํŒจํ„ด์ด๋‹ค. 

 

* ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ import 

# aiohttp ์‚ฌ์šฉ ๊ฐ€๋Šฅ (Asyncio ์ง€์›)
from concurrent import futures
import timeit
from urllib.request import urlopen
from concurrent.futures import ThreadPoolExecutor
import threading
import asyncio

 

* url ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ ๋ฐ ์‹œ๊ฐ„ ๊ธฐ๋ก 

urls = ['http://daum.net', 'https://google.com', 'https://apple.com', 'https://tistory.com', 'https://github.com', 'https://naver.com']
start = timeit.default_timer()

 

 

 

* fetch ํ•จ์ˆ˜ ์„ ์–ธ 

async def fetch(url, executor):
    print('Thread Name : ', threading.current_thread().getName(), 'Start', url)  # ํ˜„์žฌ ์“ฐ๋ž˜๋“œ๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์•Œ๋ ค์คŒ
    res = await loop.run_in_executor(executor, urlopen, url) # (์ผ๊พผ, ์ผ๊พผ์ด ์ฒ˜๋ฆฌํ•  ํ•จ์ˆ˜, ํ•ด์•ผ๋  ์ผ (์‚ฌ์ดํŠธ)) / loop ์•ˆ์—๋Š” futures๊ฐ€ ๋“ค์–ด๊ฐ€ ์žˆ์Œ . 
    print('Thread Name : ', threading.current_thread().getName(), 'Done', url)
    return res.read()[0:5]

https://silvercoding.tistory.com/30

 

[python ์‹ฌํ™”] 13. ์ฝ”๋ฃจํ‹ด (coroutine) , yield from

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ) <์ด์ „ ๊ธ€> https://silvercoding.tistory.com/29 https://silvercoding.tistory.com/28 https://silvercoding.tistory.com/27 https://silvercoding.tistory.com/26..

silvercoding.tistory.com

์œ„์˜ ํฌ์ŠคํŒ…์—์„œ yield from์„ ๊ณต๋ถ€ํ•˜์˜€๋‹ค. ๋ฒ„์ „ 3.7? ์ด์ƒ ๋ถ€ํ„ฐ await ์œผ๋กœ ๋ฐ”๋€Œ์—ˆ๊ณ  , ๋ณธ ํฌ์ŠคํŒ…์—์„œ๋Š” await์„ ์‚ฌ์šฉํ•œ๋‹ค. 

 

<loop ์ฐธ๊ณ  ๋ฌธ์„œ>

https://docs.python.org/ko/3/library/asyncio-eventloop.html

 

์ด๋ฒคํŠธ ๋ฃจํ”„ — Python 3.9.6 ๋ฌธ์„œ

์ด๋ฒคํŠธ ๋ฃจํ”„ ์†Œ์Šค ์ฝ”๋“œ: Lib/asyncio/events.py, Lib/asyncio/base_events.py ๋จธ๋ฆฌ๋ง ์ด๋ฒคํŠธ ๋ฃจํ”„๋Š” ๋ชจ๋“  asyncio ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์˜ ํ•ต์‹ฌ์ž…๋‹ˆ๋‹ค. ์ด๋ฒคํŠธ ๋ฃจํ”„๋Š” ๋น„๋™๊ธฐ ํƒœ์Šคํฌ ๋ฐ ์ฝœ๋ฐฑ์„ ์‹คํ–‰ํ•˜๊ณ  ๋„คํŠธ์›Œํฌ IO ์—ฐ์‚ฐ

docs.python.org

 

 

* main ํ•จ์ˆ˜ ์„ ์–ธ 

async def main():
    # ์“ฐ๋ ˆ๋“œ ํ’€ ์ƒ์„ฑ 
    executor = ThreadPoolExecutor(max_workers=10)
    # asyncio.ensure_future 
    futures = [asyncio.ensure_future(fetch(url, executor)) for url in urls]
    # await = yield from 
    rst = await asyncio.gather(*futures)  # unpacking

    print()
    print('Result : ', rst)

์ง€๋Šฅํ˜• ๋ฆฌ์ŠคํŠธ์ธ futures๋ฅผ ์„ ์–ธํ•œ๋‹ค. asyncio.ensure_future์— fetchํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•œ๋‹ค. 

 

 

 

* ๊ฒฐ๊ณผ ์‹คํ–‰ 

if __name__ == '__main__':
    # ๋ฃจํ”„ ์ƒ์„ฑ 
    loop = asyncio.get_event_loop()
    # ๋ฃจํ”„ ๋Œ€๊ธฐ 
    loop.run_until_complete(main())
    # ํ•จ์ˆ˜ ์‹คํ–‰ 
    main()   

    # ์™„๋ฃŒ์‹œ๊ฐ„ - ์‹œ์ž‘์‹œ๊ฐ„
    duration = timeit.default_timer() - start

    # ์ด ์‹คํ–‰์‹œ๊ฐ„   
    print('Total Time', duration)

 Thread Name :  MainThread Start http://daum.net 
 Thread Name :  MainThread Start https://google.com 
 Thread Name :  MainThread Start https://apple.com 
 Thread Name :  MainThread Start https://tistory.com 
 Thread Name :  MainThread Start https://github.com 
 Thread Name :  MainThread Start https://naver.com 
 Thread Name :  MainThread Done https://github.com 
 Thread Name :  MainThread Done https://apple.com 
 Thread Name :  MainThread Done https://naver.com 
 Thread Name :  MainThread Done https://tistory.com 
 Thread Name :  MainThread Done http://daum.net 
 Thread Name :  MainThread Done https://google.com 

 Result :  [b'<!DOC', b'<!doc', b'\n\t\n\n\n', b'\n\t<!d', b'\n\n\n\n\n', b'\n<!do'] 
 c:/Users/./Desktop/python_high/chapter06_04_03.py:57: RuntimeWarning: coroutine 'main' was never awaited 
  main() 
 RuntimeWarning: Enable tracemalloc to get the object allocation traceback       
 Total Time 1.3151724 

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ˆœ์„œ์— ์ƒ๊ด€ ์—†์ด ์ผ์„ ํ•˜๋ฉฐ, ์‹œ๊ฐ„์€ 2๋ฒˆ๋ฐฉ๋ฒ•๋ณด๋‹ค๋Š” ์˜ค๋ž˜ ๊ฑธ๋ฆฌ์ง€๋งŒ ๊ทธ๋ž˜๋„ 1.3์ดˆ๋กœ ๋น ๋ฅด๊ฒŒ ์‹คํ–‰๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

 

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

 

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/30

 

[python ์‹ฌํ™”] 13. ์ฝ”๋ฃจํ‹ด (coroutine) , yield from

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ) <์ด์ „ ๊ธ€> https://silvercoding.tistory.com/29 https://silvercoding.tistory.com/28 https://silvercoding.tistory.com/27 https://silvercoding.tistory.com/26..

silvercoding.tistory.com

 

* Future ๋™์‹œ์„ฑ / ๋น„๋™๊ธฐ ์ž‘์—… ์‹คํ–‰ 

์ง€์—ฐ์‹œ๊ฐ„ (Block) CPU ๋ฐ ๋ฆฌ์†Œ์Šค ๋‚ญ๋น„ ๋ฐฉ์ง€ -> (FILE) Network I/O๊ด€๋ จ ์ž‘์—… ๋™์‹œ์„ฑ ํ™œ์šฉ ๊ถŒ์žฅ 

: ์ ํ•ฉํ•œ ์ž‘์—…์ผ ๊ฒฝ์šฐ ์ˆœ์ฐจ ์ง„ํ–‰๋ณด๋‹ค ์••๋„์ ์œผ๋กœ ์„ฑ๋Šฅ ํ–ฅ์ƒ 

 

 

 

* ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ ์‹ค์Šตํ•  3๊ฐ€์ง€ 

1. ์ˆœ์ฐจ ์‹คํ–‰ 

2. concurrent.futures ๋ฐฉ๋ฒ•1 

3. concurrent.futures ๋ฐฉ๋ฒ• 2 

 

import os 
import time # ์„ฑ๋Šฅ์ธก์ • 
import sys
import csv
# ๊ตญ๊ฐ€ ์ •๋ณด 
NATION_LS = ('Singapore Germany Israel Norway Italy Canada France Spain Maxico').split()
# ์ดˆ๊ธฐ CSV ์œ„์น˜ 
TARGET_CSV = './resources/nations.csv'
# ์ €์žฅ ํด๋” ์œ„์น˜ 
DEST_DIR = './csvs'
# CSV ํ—ค๋” ๊ธฐ์ดˆ ์ •๋ณด 
HEADER = ['Region','Country','Item Type','Sales Channel','Order Priority','Order Date','Order ID','Ship Date','Units Sold','Unit Price','Unit Cost','Total Revenue','Total Cost','Total Profit']

<csv ํŒŒ์ผ : ํŒจ์ŠคํŠธ ์บ ํผ์Šค ์ œ๊ณต>

NATION_LS : ์‚ฌ์šฉํ•  ๊ตญ๊ฐ€ ์ด๋ฆ„ ๋ฆฌ์ŠคํŠธ 

TARGET_CSV : csvํŒŒ์ผ์ด ์žˆ๋Š” ์œ„์น˜ ๊ฒฝ๋กœ 

DEST_DIR : ์ตœ์ข…์ ์œผ๋กœ csvํŒŒ์ผ์ด ์ €์žฅ๋  ํด๋” ์œ„์น˜ ๊ฒฝ๋กœ 

HEADER : csv์˜ ํ—ค๋”๋ฅผ ์ถ”์ถœ ํ•ด๋†“์€ ๋ฆฌ์ŠคํŠธ 

 

 

 

- 1. ์ˆœ์ฐจ์‹คํ–‰ ์˜ˆ์ œ 

ํฌ์ŠคํŒ…์—์„œ๋Š” ์ฐจ๋ก€๋Œ€๋กœ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์ง€๋งŒ ๊ณต๋ถ€ํ•  ๋•Œ๋Š” ๋’ค์—์„œ๋ถ€ํ„ฐ( (6) ๋ถ€ํ„ฐ ) ๊ฑฐ์Šฌ๋Ÿฌ์˜ค๋ฉฐ ๊ณต๋ถ€ํ•˜๊ธฐ

 

 

(1) ๊ตญ๊ฐ€๋ณ„ CSV ํŒŒ์ผ ์ €์žฅ 

def save_csv(data, filename):
    # ์ตœ์ข… ๊ฒฝ๋กœ ์ƒ์„ฑ 
    path = os.path.join(DEST_DIR, filename)

    with open(path, 'w', newline="") as fp:
        writer = csv.DictWriter(fp, fieldnames=HEADER)
        # Header Write
        writer.writeheader()
        # Dict to CSV write
        for row in data: 
            writer.writerow(row)

์ตœ์ข… csvํŒŒ์ผ์„ ์ €์žฅํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. ๊ตญ๊ฐ€๋ณ„๋กœ ๋งŒ๋“ค์–ด์ง„ ๋ฐ์ดํ„ฐ(data)์™€ ์ƒ์„ฑํ•  ํŒŒ์ผ์ด๋ฆ„(filename)์„ ์ธ์ž๋กœ ๋ฐ›๋Š”๋‹ค. 

 

 

(2) ๊ตญ๊ฐ€๋ณ„ ๋ถ„๋ฆฌ 

def get_sales_data(nt):
    with open(TARGET_CSV, 'r') as f:
        reader = csv.DictReader(f)
        # Dict๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ์ ์žฌ 
        data = []
        # Header ํ™•์ธ 
        # print(reader.fieldnames)
        for r in reader:
            # OrderedDict ํ™•์ธ 
            # print(r)
            # ์กฐ๊ฑด์— ๋งž๋Š” ๊ตญ๊ฐ€๋งŒ ์‚ฝ์ž… 
            if r['Country'] == nt:
                data.append(r)
    return data

๊ตญ๊ฐ€์˜ ์ด๋ฆ„(nt)์„ ์ธ์ž๋กœ ๋ฐ›๋Š”๋‹ค. csvํŒŒ์ผ์„ ์—ด์–ด์„œ DictReader๋กœ reader์— ๋ฐ›์•„์ค€๋‹ค. Country๊ฐ€ nt๊ตญ๊ฐ€์ผ ๋•Œ data๋ฆฌ์ŠคํŠธ์— r์„ ๋„ฃ์–ด์ค€๋‹ค.  ์ด๋ ‡๊ฒŒ ๊ตญ๊ฐ€๋ณ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌํ•ด ์ฃผ๋Š” ํ•จ์ˆ˜์ด๋‹ค. 

 

 

(3) ์ค‘๊ฐ„ ์ƒํ™ฉ ์ถœ๋ ฅ

def show(text):
    print(text, end=' ')
    # ์ค‘๊ฐ„ ์ถœ๋ ฅ (๋ฒ„ํผ ๋น„์šฐ๊ธฐ)
    sys.stdout.flush()

๊ตญ๊ฐ€์˜ ์ด๋ฆ„์„ ๋ฐ›์•„์„œ ํ„ฐ๋ฏธ๋„์— ์ถœ๋ ฅํ•ด ์ฃผ์–ด ์ง„ํ–‰์ƒํ™ฉ์„ ์•Œ๋ ค์ฃผ๋Š” ํ•จ์ˆ˜์ด๋‹ค. 

 

 

(4) ๊ตญ๊ฐ€ ๋ณ„ ๋ถ„๋ฆฌ ํ•จ์ˆ˜ ์‹คํ–‰ - (1) , (2) , (3) ํ•จ์ˆ˜ ์‚ฌ์šฉ

def separate_many(nt_list):
    for nt in sorted(nt_list):
        # ๋ถ„๋ฆฌ ๋ฐ์ดํ„ฐ 
        data = get_sales_data(nt)
        # ์ƒํ™ฉ ์ถœ๋ ฅ 
        show(nt)
        # ํŒŒ์ผ ์ €์žฅ 
        save_csv(data, nt.lower() + '.csv')

    return len(nt_list)

์—ฌ๊ธฐ์„œ for๋ฌธ์œผ๋กœ ๊ฐ ๊ตญ๊ฐ€๋ณ„ ์ด๋ฆ„์„ ์•ž์—์„œ ๋ณด์•˜๋˜ get_sales_data์— ๋„ฃ์–ด์ฃผ์–ด ๋ถ„๋ฆฌ๋œ ๋ฐ์ดํ„ฐ๋ฅผ csvํŒŒ์ผ๋กœ ์ €์žฅํ•ด์ฃผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ ๋‹ค. showํ•จ์ˆ˜๋กœ ์ง„ํ–‰์ƒํ™ฉ์„ ์ถœ๋ ฅํ•˜๊ณ , save_csvํ•จ์ˆ˜๋กœ ๊ฐ ๊ตญ๊ฐ€๋ณ„ csvํŒŒ์ผ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•œ๋‹ค. 

 

 

(5) ์‹œ๊ฐ„ ์ธก์ • ๋ฐ ๋ฉ”์ธํ•จ์ˆ˜ - (4) ํ•จ์ˆ˜ ์‚ฌ์šฉ

def main(separate_many):
    # ์‹œ์ž‘ ์‹œ๊ฐ„ 
    start_tm = time.time()
    # ๊ฒฐ๊ณผ ๊ฑด์ˆ˜ 
    result_cnt = separate_many(NATION_LS)
    # ์ข…๋ฃŒ ์‹œ๊ฐ„ 
    end_tm = time.time() - start_tm 

    msg = '\n{} csv separated in {:.2f}s'

    # ์ตœ์ข… ๊ฒฐ๊ณผ ์ถœ๋ ฅ 
    print(msg.format(result_cnt, end_tm))

๋“œ๋””์–ด mainํ•จ์ˆ˜์—์„œ๋Š” separate_manyํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตญ๊ฐ€๋ณ„ csvํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๋„๋ก ํ•˜๊ณ  , ์ด์— ๋Œ€ํ•œ ์‹œ๊ฐ„์„ ์ธก์ •ํ•˜์—ฌ ์ถœ๋ ฅํ•ด์ค€๋‹ค. 

 

 

(6) ์‹คํ–‰ - (5) ํ•จ์ˆ˜ ์‚ฌ์šฉ

if __name__ == '__main__':
    main(separate_many)

Canada France Germany Israel Italy Maxico Norway Singapore Spain
9 csv separated in 21.09s

์ด๋ ‡๊ฒŒ mainํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ์ฃผ๋ฉด ์•ž์—์„œ ๋งŒ๋“ค์–ด ๋†“์•˜๋˜ ํ•จ์ˆ˜๋“ค์ด ๋ชจ๋‘ ์‹คํ–‰๋˜๋ฉฐ 

๊ตญ๊ฐ€๋ณ„ csv ํŒŒ์ผ์ด ๋ชจ๋‘ ๋งŒ๋“ค์–ด ์ง„๋‹ค. 

 

 

 

 

 

 

 

 

 

 

 

 

*** Google Python GIL (Global Interpreter Lock) 

: GIL์€ ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์˜ ์Šค๋ ˆ๋“œ๋งŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ์ธํ„ฐํ”„๋ฆฌํ„ฐ ์ž์ฒด์—์„œ ๋ฝ์„ ๊ฑฐ๋Š” ๊ฒƒ์ด๋‹ค. 

 

<python GIL ๊ด€๋ จ๋ฌธ์„œ>

https://wiki.python.org/moin/GlobalInterpreterLock

 

GlobalInterpreterLock - Python Wiki

In CPython, the global interpreter lock, or GIL, is a mutex that protects access to Python objects, preventing multiple threads from executing Python bytecodes at once. The GIL prevents race conditions and ensures thread safety. A nice explanation of how t

wiki.python.org

 

 

- 2. concurrent.futures ๋ฐฉ๋ฒ•1 (ThreadPoolExecutor, ProcessPoolExecutor)

<concurent.futures ์ฐธ๊ณ ๋ฌธ์„œ>

https://docs.python.org/ko/3.7/library/concurrent.futures.html

 

concurrent.futures — ๋ณ‘๋ ฌ ์ž‘์—… ์‹คํ–‰ํ•˜๊ธฐ — Python 3.7.11 ๋ฌธ์„œ

์†Œ์Šค ์ฝ”๋“œ: Lib/concurrent/futures/thread.py์™€ Lib/concurrent/futures/process.py concurrent.futures ๋ชจ๋“ˆ์€ ๋น„๋™๊ธฐ์ ์œผ๋กœ ์ฝœ๋Ÿฌ๋ธ”์„ ์‹คํ–‰ํ•˜๋Š” ๊ณ ์ˆ˜์ค€ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋น„๋™๊ธฐ ์‹คํ–‰์€ (ThreadPoolExecutor๋ฅผ ์‚ฌ์šฉ

docs.python.org

: map() ์‚ฌ์šฉ

: ์„œ๋กœ ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ ๋˜๋Š” ํ”„๋กœ์„ธ์Šค์—์„œ ์‹คํ–‰ ๊ฐ€๋Šฅ 

: ๋‚ด๋ถ€ ๊ณผ์ •์„ ์•Œ ํ•„์š”๊ฐ€ ์—†์œผ๋ฉฐ , ๊ณ ์ˆ˜์ค€์œผ๋กœ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•จ . 

...
from concurrent import futures

concurrent์˜ futres ๋ฅผ ์ถ”๊ฐ€๋กœ import ํ•ด์ค€๋‹ค. 

 

 

* (1) - (3) ์˜ ํ•จ์ˆ˜๋Š” ์•ž๊ณผ ๋™์ผํ•˜๋‹ค. 

 

(1) ๊ตญ๊ฐ€๋ณ„ CSV ํŒŒ์ผ ์ €์žฅ 

def save_csv(data, filename):
    # ์ตœ์ข… ๊ฒฝ๋กœ ์ƒ์„ฑ 
    path = os.path.join(DEST_DIR, filename)

    with open(path, 'w', newline="") as fp:
        writer = csv.DictWriter(fp, fieldnames=HEADER)
        # Header Write
        writer.writeheader()
        # Dict to CSV write
        for row in data: 
            writer.writerow(row)

(2) ๊ตญ๊ฐ€๋ณ„ ๋ถ„๋ฆฌ

def get_sales_data(nt):
    with open(TARGET_CSV, 'r') as f:
        reader = csv.DictReader(f)
        # Dict๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ์ ์žฌ 
        data = []
        # Header ํ™•์ธ 
        # print(reader.fieldnames)
        for r in reader:
            # OrderedDict ํ™•์ธ 
            # print(r)
            # ์กฐ๊ฑด์— ๋งž๋Š” ๊ตญ๊ฐ€๋งŒ ์‚ฝ์ž… 
            if r['Country'] == nt:
                data.append(r)
    return data

(3) ์ค‘๊ฐ„ ์ƒํ™ฉ ์ถœ๋ ฅ

def show(text):
    print(text, end=' ')
    # ์ค‘๊ฐ„ ์ถœ๋ ฅ (๋ฒ„ํผ ๋น„์šฐ๊ธฐ)
    sys.stdout.flush()

 

 

(4) ๊ตญ๊ฐ€ ๋ณ„ ๋ถ„๋ฆฌ ํ•จ์ˆ˜ ์‹คํ–‰ - (1) , (2) , (3) ํ•จ์ˆ˜ ์‚ฌ์šฉ

def separate_many(nt):
    # ๋ถ„๋ฆฌ ๋ฐ์ดํ„ฐ 
    data = get_sales_data(nt)
    # ์ƒํ™ฉ ์ถœ๋ ฅ 
    show(nt)
    # ํŒŒ์ผ ์ €์žฅ 
    save_csv(data, nt.lower() + '.csv')

    return nt

1๋ฒˆ ๋ฐฉ๋ฒ•์ธ ์ˆœ์ฐจ์‹คํ–‰์—์„œ๋Š” for๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ๊ตญ๊ฐ€๋ณ„ csvํŒŒ์ผ์„ ๋งŒ๋“ค์–ด๋ƒˆ๋‹ค. 2๋ฒˆ ๋ฐฉ๋ฒ•์—์„œ๋Š” mapํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตญ๊ฐ€ ์ด๋ฆ„ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ›์ง€ ์•Š๊ณ  , ๊ตญ๊ฐ€์ด๋ฆ„ ํ•˜๋‚˜์”ฉ์„ ๋ฐ›์•„ csvํŒŒ์ผ๋กœ ์ €์žฅํ•œ๋‹ค.  ์ตœ์ข…์ ์œผ๋กœ ๊ตญ๊ฐ€์ด๋ฆ„ ํ•˜๋‚˜(nt)๋ฅผ ๋ฆฌํ„ดํ•˜๋„๋ก ๋งŒ๋“ ๋‹ค.

 

 

 

(5) ์‹œ๊ฐ„ ์ธก์ • ๋ฐ ๋ฉ”์ธ ํ•จ์ˆ˜

futures.ThreadPoolExecutor() / futures.ProcessPoolExecutor 

 

 - ThreadPoolExecutor : GIL ์ข…์† 

def main(separate_many):
    # worker ๊ฐœ์ˆ˜
    worker = min(20, len(NATION_LS))
    # ์‹œ์ž‘ ์‹œ๊ฐ„
    start_tm = time.time()
    # ๊ฒฐ๊ณผ ๊ฑด์ˆ˜
    with futures.ThreadPoolExecutor(worker) as excutor:
        # map -> ์ž‘์—… ์ˆœ์„œ ์œ ์ง€, ์ฆ‰์‹œ ์‹คํ–‰
        result = excutor.map(separate_many, sorted(NATION_LS))
    # ์ข…๋ฃŒ ์‹œ๊ฐ„
    end_tm = time.time() - start_tm

    msg = '\n{} csv separated in {:.2f}s'
    # ์ตœ์ข… ๊ฒฐ๊ณผ ์ถœ๋ ฅ
    print(msg.format(len(list(result)), end_tm))
    
    
# ์‹คํ–‰
if __name__ == '__main__':
    main(separate_many)

 France Norway Spain Germany Maxico Canada Singapore Israel Italy 
 9 csv separated in 21.61s 

worker ๋ผ๋Š” ์ผ๊พผ ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ๊ณ , ThreadPoolExecutor()์˜ ์ธ์ž์— ๋„ฃ์–ด์ค€๋‹ค. ์—ฌ๊ธฐ์„œ result๋Š” ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ๊ฐ์ฒด๋กœ ์ €์žฅ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ์ตœ์ข…๊ฒฐ๊ณผ ์ถœ๋ ฅ์—์„œ ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด๋ฅผ ์•Œ๊ธฐ ์œ„ํ•ด result๋ฅผ list ํƒ€์ž…์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ์–ด์•ผ ํ•œ๋‹ค. 

 

๋งŒ์•ฝ ๊ทธ๋ƒฅ len(result)๋กœ ์ž‘์„ฑํ•œ๋‹ค๋ฉด !

 File "c:/Users/./Desktop/python_high/chapter06_03_02.py", line 100, in main  
    result_cnt = len(excutor.map(separate_many, sorted(NATION_LS))) 
 TypeError: object of type 'generator' has no len() 

์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋Š” len() ๋ฅผ ๊ฐ–์ง€ ์•Š๋Š”๋‹ค๋Š” ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. 

 

 

 

 

- ProcessPoolExecutor : GIL ์šฐํšŒ, ๋ณ€๊ฒฝ ํ›„ -> ๋‚ด๋ถ€์ ์œผ๋กœ os.cpu_count() ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ (์šด์˜์ฒด์ œ ์ž์ฒด์—์„œ ์šฐํšŒ์—์„œ ๊ฐ -> ์†๋„๊ฐ€ ๋น ๋ฆ„ )

def main(separate_many):
    # worker ๊ฐœ์ˆ˜
    worker = min(20, len(NATION_LS))
    # ์‹œ์ž‘ ์‹œ๊ฐ„
    start_tm = time.time()
    # ๊ฒฐ๊ณผ ๊ฑด์ˆ˜
    with futures.ProcessPoolExecutor() as excutor:
        # map -> ์ž‘์—… ์ˆœ์„œ ์œ ์ง€, ์ฆ‰์‹œ ์‹คํ–‰
        result = excutor.map(separate_many, sorted(NATION_LS))
    # ์ข…๋ฃŒ ์‹œ๊ฐ„
    end_tm = time.time() - start_tm

    msg = '\n{} csv separated in {:.2f}s'
    # ์ตœ์ข… ๊ฒฐ๊ณผ ์ถœ๋ ฅ
    print(msg.format(len(list(result)), end_tm))
    
  
# ์‹คํ–‰
if __name__ == '__main__':
    main(separate_many)

 Norway Canada Germany France Spain Singapore Italy Maxico Israel 
 9 csv separated in 8.11s 

๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ProcessPoolExecutor๋ฅผ ํ•ด๋ณธ๋‹ค. ์‹œ๊ฐ„์ด ํ›จ์”ฌ ๋‹จ์ถ•๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

 

์–ธ์ œ ProcessPoolExecutor์“ฐ๊ณ , ThreadPoolExecutor์“ฐ๊ณ , ์ฝ”๋ฃจํ‹ด์„ ์“ฐ๋Š”์ง€๋ฅผ ์ƒ๊ฐํ•ด์•ผ ํ•œ๋‹ค. ProcessPoolExecutor์“ฐ๋ฉด cpu ์‚ฌ์šฉ๋Ÿ‰์ด ๊ธ‰๊ฒฉํžˆ ์ฆ๊ฐ€ํ•˜๊ณ  ๋น ๋ฅธ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค. ๊ทธ๋ž˜์„œ ์ด๋Ÿฌํ•œ ๋ฉ€ํ‹ฐ ํ”„๋กœ์„ธ์‹ฑ์€ ๋‹จ๊ธฐ๊ฐ„์˜ ๋”ฅ๋Ÿฌ๋‹ ๊ณ ์† ์—ฐ์‚ฐ์— ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ThreadPoolExecutor์‚ฌ์šฉํ•˜๋ฉด ์†๋„๋Š” ๋” ๊ฑธ๋ฆฌ์ง€๋งŒ cpu ์‚ฌ์šฉ๋Ÿ‰์€ ์ž”์ž”ํ•˜๊ฒŒ ์ฆ๊ฐ€ํ•œ๋‹ค. ์ด ๋‘๊ฐ€์ง€์˜ ์ฐจ์ด๋ฅผ ๊ตฌ๋ถ„ํ•˜๊ณ  , ์ ์ ˆํžˆ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. 

 

 

 

 

 

 

 

 

- 3. concurrent.futures ๋ฐฉ๋ฒ•2 (map()ํ•จ์ˆ˜ ๋Œ€์‹  for๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ์„ธ๋ถ€์ ์ธ ๊ณผ์ • ์ฐ์–ด๋ณด๊ธฐ) 

 

* (1) - (3) ์˜ ํ•จ์ˆ˜๋Š” ์•ž๊ณผ ๋™์ผํ•˜๋‹ค. 

 

(4) ๊ตญ๊ฐ€ ๋ณ„ ๋ถ„๋ฆฌ ํ•จ์ˆ˜ ์‹คํ–‰ - (1) , (2) , (3) ํ•จ์ˆ˜ ์‚ฌ์šฉ

def main(separate_many):
    # worker ๊ฐœ์ˆ˜
    worker = min(20, len(NATION_LS))
    # ์‹œ์ž‘ ์‹œ๊ฐ„
    start_tm = time.time()
    # futures 
    futures_list = []
    # ๊ฒฐ๊ณผ ๊ฑด์ˆ˜
    # ProcessPoolExecutor : GIL ์šฐํšŒ, ๋ณ€๊ฒฝ ํ›„ -> ๋‚ด๋ถ€์ ์œผ๋กœ os.cpu_count() ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ (์šด์˜์ฒด์ œ ์ž์ฒด์—์„œ ์šฐํšŒ์—์„œ ๊ฐ)
    # ThreadPoolExecutor : GIL ์ข…์† 
    # with futures.ThreadPoolExecutor(worker) as excutor:
    with futures.ProcessPoolExecutor() as excutor:
        # submit -> callable ๊ฐ์ฒด ์Šค์ผ€์ฅด๋ง(์‹คํ–‰ ์˜ˆ์•ฝ) -> future 
        # Future -> as_complete(), result(), done(), cancelled() ์ฃผ๋กœ ์‚ฌ์šฉ 
        for nt in sorted(NATION_LS):
            # future ๋ฐ˜ํ™˜
            future = excutor.submit(separate_many, nt)
            # ์Šค์ผ€์ฅด๋ง
            futures_list.append(future)
            # ์ถœ๋ ฅ 
            # print('Scheduled for {} : {}'.format(nt, future))
            # print()

        for future in futures.as_completed(futures_list):
            result = future.result()
            done = future.done()
            cancelled = future.cancelled()
            # furue ๊ฒฐ๊ณผ ํ™•์ธ 
            print('Future Result : {}, Done : {}'.format(result, done))
            print('Future Cancelled : {}'.format(cancelled))

    # ์ข…๋ฃŒ ์‹œ๊ฐ„
    end_tm = time.time() - start_tm

    msg = '\n{} csv separated in {:.2f}s'
    # ์ตœ์ข… ๊ฒฐ๊ณผ ์ถœ๋ ฅ
    print(msg.format(len(futures_list), end_tm))

for๋ฌธ์œผ๋กœ ์ž‘์—…์„ ์ผ์ผ์ด ํ•˜๊ณ , ์„ธ๋ถ€์ ์œผ๋กœ ์ง„ํ–‰์ƒํ™ฉ๊ณผ ์–ด๋–ป๊ฒŒ ๋งˆ๋ฌด๋ฆฌ ๋˜์—ˆ๋Š”์ง€๋ฅผ ์•Œ๊ณ  ์‹ถ์„ ๋•Œ ์ด ๋ฐฉ๋ฒ•์„ ์“ด๋‹ค. 

2๋ฒˆ ๋ฐฉ๋ฒ•์—์„œ๋Š” map()ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ separate_many ํ•จ์ˆ˜๋กœ ๊ตญ๊ฐ€๋ณ„ csvํŒŒ์ผ์„ ์ €์žฅํ•ด ์ฃผ์—ˆ๋‹ค. 3๋ฒˆ ๋ฐฉ๋ฒ•์—์„œ๋Š” submit()ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ futre๋ณ€์ˆ˜์— ํ• ๋‹นํ•ด์ฃผ๊ณ  , ์ด future๋ฅผ futures_list์— append ํ•ด์ค€๋‹ค. 

 

futures.as_complted()์˜ ์ธ์ž์— futures_list๋ฅผ ๋„ฃ์–ด์ฃผ๊ณ , ๊ฐ๊ฐ์˜ future์— ๋Œ€ํ•œ result, done, cancelled ๊ฐ’์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. 

 

 

* ์‹คํ–‰ 

if __name__ == '__main__':
    main(separate_many)

 Germany Canada Future Result : Germany, Done : True 
 Future Cancelled : False 
 Norway Israel Future Result : Norway, Done : True 
 Italy Future Cancelled : False 
 Future Result : Canada, Done : True 
 Future Cancelled : False 
 Future Result : Israel, Done : True 
 Future Cancelled : False 
 Future Result : Italy, Done : True 
 Future Cancelled : False 
 Singapore France Future Result : Singapore, Done : True 
 Future Cancelled : False 
 Future Result : France, Done : True 
 Future Cancelled : False 
 Maxico Future Result : Maxico, Done : True 
 Future Cancelled : False 
 Spain Future Result : Spain, Done : True 
 Future Cancelled : False 

 9 csv separated in 8.60s 

์‹คํ–‰์„ ํ•ด๋ณด๋ฉด , result๋Š” ๊ฐ ๊ตญ๊ฐ€์˜ ์ด๋ฆ„(nt : separate_many์˜ ๋ฆฌํ„ด๊ฐ’), Done์€ ๋ชจ๋‘ True, Cancelled๋Š” ๋ชจ๋‘ False๊ฐ€ ๋‚˜์™€ ์ •์ƒ์ ์œผ๋กœ ์ž‘๋™ํ•œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ด์™€ ๊ฐ™์ด for๋ฌธ๊ณผ submitํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„ธ๋ถ€์ ์ธ ๋‚ด์šฉ์„ ํ™•์ธํ•  ์ˆ˜๋„ ์žˆ๋‹ค. 

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

 

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/29

 

[python ์‹ฌํ™”] 12. ๋‚ด๋ถ€ iter ํ•จ์ˆ˜, ์ œ๋„ˆ๋ ˆ์ดํ„ฐ(Generator)

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

silvercoding.tistory.com

 

 

* yield : ๋ฉ”์ธ ๋ฃจํ‹ด (์ˆœ์ฐจ์ง„ํ–‰ - ํ•˜๋‚˜์˜ ํ๋ฆ„) <-> ์„œ๋ธŒ๋ฃจํ‹ด (ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด ํ•จ์ˆ˜๊ฐ€ ์žˆ๋Š” ๊ณณ์œผ๋กœ) ์ด ๋ฃจํ‹ด ๋‘๊ฐœ๋ฅผ ๋™์‹œ์— ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๊ฒŒ yield์ด๋‹ค. 

* ์ฝ”๋ฃจํ‹ด ์ œ์–ด , ์ฝ”๋ฃจํ‹ด  ์ƒํƒœ, ์–‘๋ฐฉํ–ฅ ๊ฐ’ ์ „์†ก (์–‘๋ฐฉํ–ฅ ํ†ต์‹ ) 

* ์„œ๋ธŒ ๋ฃจํ‹ด : ๋ฉ”์ธ๋ฃจํ‹ด์—์„œ ๋ฆฌํ„ด์— ์˜ํ•ด ํ˜ธ์ถœ ๋ถ€๋ถ„์œผ๋กœ ๋Œ์•„์™€ ๋‹ค์‹œ ํ”„๋กœ์„ธ์Šค 

* ์ฝ”๋ฃจํ‹ด : ๋ฃจํ‹ด ์‹คํ–‰ ์ค‘ ๋ฉˆ์ถค ๊ฐ€๋Šฅ -> ํŠน์ • ์œ„์น˜๋กœ ๋Œ์•„๊ฐ”๋‹ค๊ฐ€ -> ๋‹ค์‹œ ์›๋ž˜ ์œ„์น˜๋กœ ๋Œ์•„์™€ ์ˆ˜ํ–‰ ๊ฐ€๋Šฅ -> ๋™์‹œ์„ฑ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ด์คŒ 

*์“ฐ๋ ˆ๋“œ : ์‹ฑ๊ธ€์“ฐ๋ ˆ๋“œ -> ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ -> ๋ณต์žกํ•˜๋‹ค! (๊ณต์œ ๋˜๋Š” ์ž์›์— ๋Œ€ํ•œ ๊ต์ฐฉ์ƒํƒœ ๋ฐœ์ƒ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์œผ๋ฏ€๋กœ ์ฃผ์˜๊นŠ๊ฒŒ ์ฝ”๋”ฉํ•ด์•ผ ํ•จ) / ์ปจํ…์ŠคํŠธ ์Šค์œ„์นญ ๋น„์šฉ ๋ฐœ์ƒ, ์ž์› ์†Œ๋น„ ๊ฐ€๋Šฅ์„ฑ ์ฆ๊ฐ€ 

 

 

- ์ฝ”๋ฃจํ‹ด ์˜ˆ์ œ1 

def coroutine1():
    print('>>> coroutine started.')
    i = yield 
    print('>>> coroutine received : {}'.format(i))

์ €๋ฒˆ ๊ธ€์—์„œ๋Š” yield i <-- ์ด๋Ÿฐ ์‹์œผ๋กœ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ , ์ด๋ฒˆ์—๋Š” i = yield ์˜ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ๋ฉ”์ธ ๋ฃจํ‹ด์—์„œ ๊ฐ’์„ ์ „๋‹ฌ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. 

 

 

* ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ์„ ์–ธ

c1 = coroutine1()
print('EX1-1 - ', c1, type(c1))

 EX1-1 -  <generator object coroutine1 at 0x000001430D3574F8> <class 'generator'> 

์ œ๋„ˆ๋ ˆ์ดํ„ฐ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์˜€๋‹ค. 

 

next(c1)
next(c1)

 >>> coroutine started. 
 >>> coroutine received : None 
 Traceback (most recent call last): 
  File "c:/Users/./Desktop/python_high/chapter06_02.py", line 35, in <module>  
    next(c1) 
 StopIteration 

์œ„์™€๊ฐ™์ด ๊ฐ’์„ ์ „์†กํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ None์„ ์ „์†กํ•œ๋‹ค. 

 

* ๊ฐ’ ์ „์†ก 

next(c1)
c1.send(100)

 >>> coroutine started. 
 >>> coroutine received : 100 
 Traceback (most recent call last): 
  File "c:/Users/./Desktop/python_high/chapter06_02.py", line 41, in <module>  
    c1.send(100) 
 StopIteration 

์ด๋ ‡๊ฒŒ next() ๋Œ€์‹  send()๋กœ ๊ฐ’์„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

* ์ž˜๋ชป๋œ ์‚ฌ์šฉ 

c2 = coroutine1()
c2.send(100) # ์˜ˆ์™ธ ๋ฐœ์ƒ

 Traceback (most recent call last): 
  File "c:/Users/./Desktop/python_high/chapter06_02.py", line 45, in <module>  
    c2.send(100) # ์˜ˆ์™ธ ๋ฐœ์ƒ 
 TypeError: can't send non-None value to a just-started generator 

next()๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š๊ณ  , ์ฒ˜์Œ๋ถ€ํ„ฐ send๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค! ( ๋’ค์—์„œ next๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š์•„๋„ ๋˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์šธ ๊ฒƒ์ด๋‹ค. )

 

 

 

 

 

 

 

 

 

- ์ฝ”๋ฃจํ‹ด ์˜ˆ์ œ 2 

* GEN_CREATED : ์ฒ˜์Œ ๋Œ€๊ธฐ ์ƒํƒœ 

* GEN_RUNNING : ์‹คํ–‰ ์ƒํƒœ 

* GEN_SUSPENDED : yield ๋Œ€๊ธฐ ์ƒํƒœ 

* GEN_CLOSED : ์‹คํ–‰ ์™„๋ฃŒ ์ƒํƒœ 

 

def coroutine2(x):
    print('>>> coroutine started : {}'.format(x))
    y = yield x 
    print('>>> coroutine received : {}'.format(y))
    z = yield x + y 
    print('>>> coroutine received : {}'.format(z))

 

c3 = coroutine2(10)

coroutine2 ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ฐ์ฒด๋ฅผ ํ• ๋‹นํ•ด ์ค€๋‹ค. x๋Š” 10์œผ๋กœ ๋„ฃ์–ด์ค€๋‹ค. 

from inspect import getgeneratorstate
from typing_extensions import Literal

print('EX1-2 - ', getgeneratorstate(c3))

print(next(c3))

print('EX1-3 - ', getgeneratorstate(c3))

print(c3.send(15))
print(c3.send(20)) # ์˜ˆ์™ธ

 EX1-2 -  GEN_CREATED 

getgeneratorstate๋ฅผ ์ด์šฉํ•˜์—ฌ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ์˜ ์ƒํƒœ๋ฅผ ๊ฐ€์ ธ์™€์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ์šฐ์„  ๊ฐ์ฒด๋งŒ ์ƒ์„ฑํ–ˆ์„ ๋•Œ ์ฒ˜์Œ ๋Œ€๊ธฐ ์ƒํƒœ์ž„์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 
 >>> coroutine started : 10 
 10 

x๊ฐ’์ด ๋ฐ˜ํ™˜๋˜๊ณ , ์ด ์ƒํƒœ์—์„œ ๋Œ€๊ธฐํ•œ๋‹ค. 
 EX1-3 -  GEN_SUSPENDED 

yield ๋Œ€๊ธฐ ์ƒํƒœ ์ž„์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 
 >>> coroutine received : 15 
 25 

๊ทธ๋‹ค์Œ send๋กœ 15๋ฅผ ๋ณด๋‚ด๋ฉด ๋ฉˆ์ถฐ์žˆ๋˜ yield ์ƒํƒœ์—์„œ y์— ๊ฐ’ 15๊ฐ€ ์ „๋‹ฌ๋œ๋‹ค. print๋ฌธ์œผ๋กœ 15๊ฐ€ ์ž˜ ์ „๋‹ฌ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๊ณ , ๊ทธ ๋‹ค์Œ yield์˜ x + y ๊ฐ€ ๋ฐ˜ํ™˜๋˜๊ณ  ์ด์ƒํƒœ์—์„œ ๋ฉˆ์ถ˜๋‹ค. 

 >>> coroutine received : 20 

๋˜ ๋‹ค์‹œ send๋กœ 20์„ ๋ณด๋‚ด๋ฉด yield ๋Œ€๊ธฐ ์ƒํƒœ์—์„œ ๋ฉˆ์ถ”์–ด ์žˆ๋˜ z์— ๊ฐ’์ด ์ „๋‹ฌ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

 Traceback (most recent call last): 
  File "c:/Users/./Desktop/python_high/chapter06_02.py", line 78, in <module>  
    print(c3.send(20)) # ์˜ˆ์™ธ 
 StopIteration 

๊ทธ๋‹ค์Œ yield๊ฐ€ ์—†์œผ๋ฏ€๋กœ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๊ฒŒ ๋œ๋‹ค. 

 

 

 

 

 

 

 

 

 

 

- ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ํŒจํ„ด

: next๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค! 

<๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ํฌ์ŠคํŒ…>

https://silvercoding.tistory.com/26

 

[python ์‹ฌํ™”] 9. ๋ณ€์ˆ˜ ๋ฒ”์œ„, Closure, Decorator

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

silvercoding.tistory.com

๋‚ด๋ถ€์ ์œผ๋กœ ๋Œ์•„๊ฐ€๋Š” ๊ฒŒ ํ•ญ์ƒ ํ—ท๊ฐˆ๋ ค์„œ ๋ณด๊ณ ์˜ค๊ธฐ

from functools import wraps

def coroutine(func):
    '''Decorator run until yield'''
    @wraps(func)
    def primer(*args, **kwargs):
        gen = func(*args, **kwargs)
        next(gen)
        return gen
    return primer

์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ฒ˜์Œ์— next๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š๊ณ  ๋ฐ”๋กœ send๋กœ ๊ฐ’์„ ๋ณด๋‚ด๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋‚ฌ๋‹ค. ์ด๋ฒˆ์—๋Š” ์œ„ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ”๋กœ ๊ฐ’์„ ์ „์†กํ•˜๋„๋ก ๋งŒ๋“ ๋‹ค. 

@coroutine
def sumer():
    total = 0 
    term = 0 
    while True:
        term = yield total
        total += term
su = sumer()

๋ฐ์ฝ”๋ ˆ์ดํ„ฐ @coroutine์„ ์ž‘์„ฑํ•ด ์ฃผ๊ณ , ๊ฐ์ฒด su๋ฅผ ์ƒ์„ฑํ•˜์˜€๋‹ค. 

print('EX2-1 - ', su.send(100))
print('EX2-2 - ', su.send(40))
print('EX2-3 - ', su.send(60))

 EX2-1 -  100 
 EX2-2 -  140 
 EX2-3 -  200 

๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋กœ ์ธํ•ด next๊ฐ€ ํ˜ธ์ถœ๋˜์—ˆ๊ณ , yield์—์„œ ๋ฉˆ์ถ”์–ด ์žˆ๋Š” ์ƒํƒœ์ด๋‹ค. ์ด ์ƒํƒœ์—์„œ send๋กœ 100์„ ์ „์†กํ•˜๋ฉด term์— 100์ด ๋“ค์–ด๊ฐ€๊ณ , total์€ 0+100=100์ด ๋˜๊ณ , ๋‹ค์‹œ yield์—์„œ total์ด ๋ฐ˜ํ™˜๋˜๊ณ  ๋ฉˆ์ถ˜๋‹ค. ๊ทธ๋‹ค์Œ send๋กœ 40์„ ์ „์†กํ•˜๋ฉด term์— 40์ด ๋“ค์–ด๊ฐ€๊ณ , total=100+40=140์ด ๋˜์–ด ๋‹ค์‹œ yield๋ฅผ ๋งŒ๋‚ ๋•Œ๊นŒ์ง€ while๋ฌธ์ด ๋Œ์•„ total์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๋ฉˆ์ถ˜๋‹ค. 60๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋Œ์•„๊ฐ€์„œ 200์ด ๋ฐ˜ํ™˜๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

 

 

 

 

 

- ์ฝ”๋ฃจํ‹ด ์˜ˆ์ œ 3 (์˜ˆ์™ธ ์ฒ˜๋ฆฌ) 

class SampleException(Exception):
    '''์„ค๋ช…์— ์‚ฌ์šฉํ•  ์˜ˆ์™ธ ์œ ํ˜•'''
def coroutine_except():
    print('>> coroutine started')
    try: 
        while True:
            try:
                x = yield
            except SampleException:
                print('-> SampleException handled. Continuing..') 
            else:
                print('-> coroutine received : {}'.format(x))
    finally:
        print('-> coroutine ending')

์œ„์—์„œ ๋งŒ๋“ค์–ด ๋†“์€ SampleException์„ ์ด์šฉํ•˜์—ฌ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค€๋‹ค. 

exe_co = coroutine_except()

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

print('EX3-1 - ', next(exe_co))
print('EX3-2 - ', exe_co.send(10))
print('EX3-3 - ', exe_co.send(100))
print('EX3-4 - ', exe_co.throw(SampleException))
print('EX3-5 - ', exe_co.send(1000))
print('EX3-6 - ', exe_co.close()) # GEN_CLOSED 
print('EX3-7 - ', exe_co.send(10)) # ์—๋Ÿฌ

 >> coroutine started 
 EX3-1 -  None 
 -> coroutine received : 10 
 EX3-2 -  None 
 -> coroutine received : 100 
 EX3-3 -  None 
 -> SampleException handled. Continuing.. 
 EX3-4 -  None 
 -> coroutine received : 1000 
 EX3-5 -  None 
 -> coroutine ending 
 EX3-6 -  None 
 Traceback (most recent call last): 
  File "c:/Users/./Desktop/python_high/chapter06_02.py", line 146, in <module>  
    print('EX3-7 - ', exe_co.send(10)) # ์—๋Ÿฌ 
 StopIteration 

EX3-4 ์—์„œ throw()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ์™ธ๋ฅผ ์žก๊ณ  ๋‹ค์‹œ yield๋กœ ๊ฐ€์„œ None์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๋ฉˆ์ถ”์–ด ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋‹ค์‹œ send๋กœ ๊ฐ’์„ ์ „์†กํ•ด๋„ ๋™์ž‘์„ ํ•˜๊ณ , ์ œ๋„ˆ๋ ˆ์ดํŠธ๋ฅผ ์•„์˜ˆ ๋๋‚ด๊ณ  ์‹ถ๋‹ค๋ฉด close()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. close()๋ฅผ ํ˜ธ์ถœ ํ•œ ํ›„ , send๋กœ ๊ฐ’์„ ์ „์†กํ•˜๋ ค๊ณ  ํ•˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

 

 

 

 

 

 

 

 

- ์ฝ”๋ฃจํ‹ด ์˜ˆ์ œ 4 (return) 

def averager_re():
    total = 0.0
    cnt = 0 
    avg = None
    while True:
        term = yield
        if term is None:
            break
        total += term
        cnt += 1
        avg = total / cnt 
    return 'Average : {}'.format(avg)
avger2 = averager_re()

๊ฐ’์œผ๋กœ None์„ ๋ณด๋‚ผ ๋•Œ๊นŒ์ง€ while๋ฌธ์ด ์‹คํ–‰๋˜๋„๋ก ํ•˜๊ณ  , ๋ณด๋‚ด๋Š” ๊ฐ’์„ ๋ชจ๋‘ ๋”ํ•ด์„œ ํ‰๊ท ์„ ๋‚ด์ฃผ๋Š” ํ•จ์ˆ˜์ด๋‹ค. 

next(avger2)

avger2.send(10)
avger2.send(30)
avger2.send(50)

์œ„์™€ ๊ฐ™์ด ๊ฐ’์„ ์ „๋‹ฌํ•ด์ค€๋‹ค. 

try:
    avger2.send(None)
except StopIteration as e:
    print('EX4-1 - ', e.value)

 EX4-1 -  Average : 30.0 

None์„ ์ „์†กํ•˜์—ฌ while๋ฌธ์—์„œ ํƒˆ์ถœํ•˜๊ณ  , ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋กœ ํ•จ์ˆ˜์˜ ๋ฆฌํ„ด ๊ฐ’์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

 

 

 

 

 

 

 

 

 

- ์ฝ”๋ฃจํ‹ด ์˜ˆ์ œ 4 (yield from) (3.7 -> await

: ์ค‘์ฒฉ ์ฝ”๋ฃจํ‹ด ์ฒ˜๋ฆฌ 

 

(1) yield from ๋ฏธ์‚ฌ์šฉ 

def gen1():
    for x in 'AB':
        yield x 
    for y in range(1, 4):
        yield y

์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•ด ์ค€๋‹ค.

t1 = gen1()
print('EX5-1 - ', next(t1))
print('EX5-2 - ', next(t1))
print('EX5-3 - ', next(t1))
print('EX5-4 - ', next(t1))
print('EX5-5 - ', next(t1))
print('EX5-6 - ', next(t1)) # StopIteration

 EX5-1 -  A 
 EX5-2 -  B 
 EX5-3 -  1 
 EX5-4 -  2 
 EX5-5 -  3 
 Traceback (most recent call last): 
  File "c:/Users/./Desktop/python_high/chapter06_02.py", line 198, in <module> 
    print('EX5-6 - ', next(t1)) # StopIteration 
 StopIteration 

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

t2 = gen1()
print('EX5-7 - ', list(t2))

 EX5-7 -  ['A', 'B', 1, 2, 3] 

์ด๋ ‡๊ฒŒ ๋ฆฌ์ŠคํŠธ ๊ฐ์ฒด๋กœ ๋ฐ›์•„์ค„ ์ˆ˜๋„ ์žˆ๋‹ค. 

 

 

 

 

(2) yield from ์‚ฌ์šฉ 

def gen2():
    yield from 'AB'
    yield from range(1, 4)

์ด์™€ ๊ฐ™์ด yield from์„ ์‚ฌ์šฉํ•˜๋ฉด ์œ„์˜ ์˜ˆ์‹œ ์ฒ˜๋Ÿผ for๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋˜๊ณ  , ๊ฐ„๋‹จํ•˜๊ฒŒ ๋‘ ์ค„๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. 

t3 = gen2()
print('EX6-1 - ', next(t3))
print('EX6-2 - ', next(t3))
print('EX6-3 - ', next(t3))
print('EX6-4 - ', next(t3))
print('EX6-5 - ', next(t3))
print('EX5-6 - ', next(t3)) # StopIteration

 EX6-1 -  A 
 EX6-2 -  B 
 EX6-3 -  1 
 EX6-4 -  2 
 EX6-5 -  3 
 Traceback (most recent call last): 
  File "c:/Users/sooki/Desktop/python_high/chapter06_02.py", line 217, in <module> 
    print('EX5-6 - ', next(t3)) # StopIteration 
 StopIteration 

(1)๋ฒˆ ์˜ˆ์ œ์™€ ๋˜‘๊ฐ™์ด ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

t4 = gen2()
print('EX6-7 - ', list(t4))

 EX6-7 -  ['A', 'B', 1, 2, 3] 

list๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋™์ผํ•˜๊ฒŒ ๋‚˜์˜จ๋‹ค. 

 

 

 

 

 

* yield from ์˜ˆ์ œ +1 

def gen3_sub():
    print('Sub coroutine')
    x = yield 10 
    print('Recv: ', str(x))
    x = yield 100
    print('Recv: ', str(x))
    
def gen4_main():
    yield from gen3_sub()

์œ„์™€ ๊ฐ™์ด sub coroutine์œผ๋กœ ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•ด ๋†“๊ณ  , main coroutine์—์„œ yield from ์œผ๋กœ ์ž‘์—…์„ ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค. 

t5 = gen4_main()

print('EX7-1 - ', next(t5))
print('EX7-2 - ', t5.send(7))
print('EX7-3 - ', t5.send(77)) # ์˜ค๋ฅ˜

 Sub coroutine 
 EX7-1 -  10 
 Recv:  7 
 EX7-2 -  100 
 Recv:  77 
 Traceback (most recent call last): 
  File "c:/Users/sooki/Desktop/python_high/chapter06_02.py", line 247, in <module> 
    print('EX7-2 - ', t5.send(77)) 
 StopIteration 

 

 

 

 

 

 

 

 

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

 

 

<์ด์ „ ๊ธ€>

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