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

Python/Python

[Python ์ค‘๊ธ‰] Python ์‹œํ€€์Šค

๋ฐ˜์‘ํ˜•

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

 

Python

 

์ด๋ฒˆ ํฌ์ŠคํŒ…์€ ํŒŒ์ด์ฌ์˜ ์‹œํ€€์Šค ์ž๋ฃŒ๊ตฌ์กฐ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ณ  ์ด์— ๋Œ€ํ•ด ์‹ค์Šต๋„ ํ•ด๋ณด์ž. ์šฐ์„  ํŒŒ์ด์ฌ ๋ฐ์ดํ„ฐ ํƒ€์—ก์— ๋Œ€ํ•ด ์„ธ๋ถ€์ ์œผ๋กœ ๋ถ„๋ฅ˜ํ•ด๋ณด์ž.

 

  • 1๋ฒˆ์งธ ๊ธฐ์ค€ : ์ž๋ฃŒ ๊ตฌ์„ฑ ํ˜•ํƒœ
    • ์ปจํ…Œ์ด๋„ˆ - ์„œ๋กœ ๋‹ค๋ฅธ ์ž๋ฃŒํ˜• ๊ตฌ์„ฑ ๊ฐ€๋Šฅ : ๋ฆฌ์ŠคํŠธ, ํŠœํ”Œ, collections์˜ deque
    • ํ”Œ๋žซํ˜•(Flat) - ํ•œ ๊ฐœ์˜ ์ž๋ฃŒํ˜•์œผ๋กœ ๊ตฌ์„ฑ : ๋ฌธ์ž์—ด(str), bytes, bytes array, array, memoryview
  • 2๋ฒˆ์งธ ๊ธฐ์ค€ : ์ž๋ฃŒ ๊ตฌ์กฐ์˜ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ ์—ฌ๋ถ€
    • ๊ฐ€๋ณ€ํ˜• ์ž๋ฃŒ๊ตฌ์กฐ : ๋ฆฌ์ŠคํŠธ, bytes array, array, memoryview, deque
    • ๋ถˆ๋ณ€ํ˜• ์ž๋ฃŒ๊ตฌ์กฐ : ํŠœํ”Œ, ๋ฌธ์ž์—ด(str), bytes

 

# ord(string)์€ ๋ฌธ์ž์—ด์˜ ์œ ๋‹ˆ์ฝ”๋“œ ๋ฒˆํ˜ธ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์คŒ
# 1.์ผ๋ฐ˜ list append
chars = '!@$##%$^%$^&'
code_lst1 = []
for c in chars:
    code_lst1.append(ord(c))
print("์ผ๋ฐ˜ list append: ", code_lst1)
# List comprehension => ๋ฐ์ดํ„ฐ ์–‘์ด ์ปค์ง€๋ฉด์„œ ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์ด ๋” ์†๋„๊ฐ€ ์šฐ์„ธํ•˜๋‹ค๊ณ  ํ•จ!
code_lst2 = [ord(c) for c in chars]
print("list comprehension: ", code_lst2)
print('-'*50)

 

  • list comprehension ์ด ์ผ๋ฐ˜ for ๋ฌธ์„ ์‚ฌ์šฉํ•œ ๊ฒƒ๋ณด๋‹ค ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ์•„์งˆ์ˆ˜๋ก ์†๋„๊ฐ€ ๋” ์šฐ์„ธ

๋‹ค์Œ์€ iterator ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” map, filter ํ•จ์ˆ˜๋ฅผ ์•Œ์•„๋ณด์ž.

 

# Map, Filter๋Š” iterator๋ฅผ ๋งŒ๋“ฆ!
# filter ํ•จ์ˆ˜๋Š” filter(ํ•จ์ˆ˜, iterableํ•œ ๊ฐ์ฒด) => iterator๊ฐ€ ๊ฒฐ๊ณผ๊ธฐ ๋•Œ๋ฌธ์— next()๋ฅผ ์ด์šฉํ•ด ์ถœ๋ ฅ ๊ฐ€๋Šฅ
filter_ = filter(lambda x: x > 40, [ord(c) for c in chars])
print("filter์— list๊ฐ์‹ธ์ค€ ํ›„ :", list(filter_))
# list๋กœ ๊ฐ์‹ธ์„œ listํ˜•ํƒœ๋กœ ๋งŒ๋“ค์–ด์ฃผ์ž

# map ํ•จ์ˆ˜
map_ = map(ord, chars)
print([ord(c) for c in chars])
print('์ฒซ๋ฒˆ์งธ ์š”์†Œ:', next(map_))
print('๋‘๋ฒˆ์งธ ์š”์†Œ:', next(map_))
print('์„ธ๋ฒˆ์งธ ์š”์†Œ:', next(map_))

# list comprehension VS Map + Filter
code_lst3 = [ord(c) for c in chars if ord(c) > 40]
code_lst4 = list(filter(lambda x: x > 40, map(ord, chars)))
print("list comprehension: ", code_lst3)
print("Map + Filiter: ", code_lst4)
print('-'*50)

 

  • filter(ํ•จ์ˆ˜, iterativeํ•œ ๊ฐ์ฒด) ๋ฅผ ์ธ์ž๋กœ ๋„ฃ์–ด์ฃผ๋ฉฐ ํ•ด๋‹น ํ•จ์ˆ˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋ฐ์ดํ„ฐ๋“ค์„ ๋ฐ˜ํ™˜ํ•˜๋ ค๋ฉด next() ๋ฉ”์†Œ๋“œ๋กœ ํ•˜๋‚˜์”ฉ ๋ฐ˜ํ™˜ํ•˜๊ฑฐ๋‚˜ list()๋กœ ๊ฐ์‹ธ์ฃผ์–ด list ๊ฐ์ฒด๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Œ
  • map(ํ•จ์ˆ˜, iterativeํ•œ ๊ฐ์ฒด) ๋ฅผ ์ธ์ž๋กœ ๋„ฃ์–ด์ฃผ๋ฉฐ list๋กœ ๊ฐ์‹ธ์ฃผ์–ด ๋ฐ์ดํ„ฐ๊ฐ€ ๋‹ด๊ธด ๊ฒƒ๋“ค์„ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๊ณ  next() ๋ฉ”์†Œ๋“œ๋กœ ํ•˜๋‚˜์”ฉ ์ถœ๋ ฅ ๊ฐ€๋Šฅ

๋‹ค์Œ์€ Generator๋ฅผ ์ƒ์„ฑํ•ด๋ณด์ž. ์ œ๋„ค๋ ˆ์ดํ„ฐ๋Š” iterator์™€ ๊ฐ™์ด ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ ˆ์•ฝํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•  ์ค€๋น„๋งŒ ๋งž์ถฐ๋†“์€ ์ƒํƒœ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰, list comprehension ์ฒ˜๋Ÿผ ํ•œ ๋ฒˆ์— ๋ฐ์ดํ„ฐ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ฆฌ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ next() ๋ฉ”์†Œ๋“œ๋ฅผ ํ™œ์šฉํ•ด ํ•˜๋‚˜์”ฉ๋งŒ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ ค๋†“๋„๋ก ํ•˜์—ฌ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ ˆ์•ฝํ•  ์ˆ˜ ์žˆ์Œ

 

# Generator ์ƒ์„ฑ -> ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ ˆ์•ฝํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์ง์ „ ์ƒ์„ฑํ•  ์ค€๋น„๋งŒ ๋งž์ถฐ๋†“์€ ์ƒํƒœ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Œ!
# ํ•œ๊ฐœ์˜ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ฐ–์œผ๋ฉด์„œ ๊ฐ€๋ณ€์ ์ธ array ์ž๋ฃŒ๊ตฌ์กฐ ์‚ฌ์šฉํ•ด๋ณด๊ธฐ
# Generator๋Š” list comprehension ํ‘œํ˜„์‹์—์„œ ๋Œ€๊ด„ํ˜ธ[] -> ์†Œ๊ด„ํ˜ธ()๋กœ ๋ฐ”๊ฟ”!

tuple_g = (ord(c) for c in chars)
print(tuple_g, type(tuple_g))
print([ord(c) for c in chars])  # list comprehension์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ๋‘ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ ค๋ฒ„๋ฆผ..!
print(next(tuple_g))
print(next(tuple_g))

# Generaotr ์ด์šฉํ•ด 4๊ฐœ์˜ ๋ฐ˜๊ณผ ๊ฐ 20๊ฐœ์˜ ๋ฒˆํ˜ธ๋ฅผ ๊ฐ–๋Š” ์ž๋ฃŒ๊ตฌ์กฐ ์ƒ์„ฑ
students_g = (f"{room}๋ฐ˜ {num}๋ฒˆ" for room in 'A B C D'.split() for num in range(1, 21))
print(students_g)
for student in students_g:
    print(student)

 

๋‹ค์Œ์€ Tuple์„ unpacking ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

 

# b, a = a, b -> ์ด๊ฒƒ๋„ ์ผ์ข…์˜ unpacking์ž„!
# *(aster)๋ฅผ ์ด์šฉํ•ด unpacking ๊ฐ€๋Šฅ
print(divmod(100, 21))  # ๋ชซ๊ณผ ๋‚˜๋จธ์ง€ ์ถœ๋ ฅ
print(divmod(*(100, 21)))  # tuple (100, 21)์„ unpack
print(*(divmod(100, 21)))  # divmod๊ฒฐ๊ณผ๊ฐ’์„ unpack
print()

x, y, *rest = range(10)
print(x, y, rest)
x, y, *rest= range(2)  # ๋ถ€์กฑํ•ด๋„ ๋นˆ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ˜ํ™˜
print(x, y, rest)
x, y, *rest = 1, 2, 3, 4, 5
print(x, y, rest)
print()
print()

 

๋‹ค์Œ์€ sorted ์™€ sort ๋ฉ”์†Œ๋“œ๋ฅผ ๋น„๊ตํ•ด๋ณด์ž. ๊ฐ€์žฅ ํฐ ์ฐจ์ด์ ์€ ์ •๋ ฌํ›„ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜์‹œํ‚ค๋Š๋ƒ ๊ธฐ์กด์˜ ๊ฐ์ฒด๋ฅผ ์ •๋ ฌ์‹œํ‚ค๊ณ  ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜์‹œํ‚ค์ง€ ์•Š๋Š๋ƒ์˜ ์ฐจ์ด์ด๋‹ค. optional๋กœ ๋“ค์–ด๊ฐ€๋Š” ์ •๋ ฌ ์ธ์ž๋Š” ๋™์ผํ•˜๋‹ค.

 

# sorted VS sort : ๊ณตํ†ต์ธ์ž๋กœ reverse, key=len/str.lower/ํ•จ์ˆ˜.. ๊ฐ€
# 1. sorted : ์ •๋ ฌ ํ›„ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜
v_list = ['bus', 'train', 'airplane', 'underground', 'car']
h_list = ['๋ฒ„์Šค', '๊ธฐ์ฐจ', '๋น„ํ–‰๊ธฐ', '์ง€ํ•˜์ฒ ', '์ž๋™์ฐจ']
print('sorted: ', sorted(v_list)) # ๋””ํดํŠธ๋Š” ์•ŒํŒŒ๋ฒณ์€ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ
print('sorted: ', sorted(h_list)) # ๋””ํดํŠธ๋Š” ํ•œ๊ธ€๋„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ
print('sorted1: ', sorted(v_list, reverse=True))
print('sorted1: ', sorted(h_list, reverse=True))
print('sorted2: ', sorted(v_list, key=len))
print('sorted2: ', sorted(h_list, key=len))
print('sorted3: ', sorted(v_list, key=lambda x: x[-1],
                          reverse=True)) # ๋ฌธ์ž์—ด ๋งˆ์ง€๋ง‰ ๊ธ€์ž ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ
print('sorted3: ', sorted(h_list, key=lambda x: x[-1],
                          reverse=True))
print()

# 2. sort : ์ •๋ ฌํ•œ ํ›„ ๊ธฐ์กด ๊ฐ์ฒด๋ฅผ ๋ณ€๊ฒฝ์‹œํ‚ด(์ƒˆ๋กœ์šด ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ ๋ถ€์—ฌํ•˜์ง€ ์•Š๊ณ  ๊ธฐ์กด ๊ฒƒ ์‚ฌ์šฉ)
print('sort1: ', v_list.sort(), v_list) # sort() ๊ธฐ์กด์˜ ๊ฒƒ์„ ๋ณ€๊ฒฝํ•˜๊ธฐ ๋•Œ๋ฌธ์— None์„ ์ถœ๋ ฅํ•จ
print('sort2: ', v_list.sort(reverse=True), v_list)
print('sort3: ', v_list.sort(key=len), v_list)
print('sort4: ', v_list.sort(key=lambda x: x[-1]), v_list)

 

๋‹ค์Œ์€ Hash table์— ๋Œ€ํ•œ ๋‚ด์šฉ์ด๋‹ค. ํŒŒ์ด์ฌ์—์„œ๋Š” Dictionary ๋ผ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋กœ ์ธํ•ด ํŽธ๋ฆฌํ•˜๊ฒŒ Hash table๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ Hash ๊ฐ’์ด๋ž€, ๋ฐ์ดํ„ฐ์˜ uniqueํ•œ key๊ฐ’์„ ์˜๋ฏธํ•˜๋ฉฐ hash() ๋ฉ”์†Œ๋“œ๋ฅผ ํ™œ์šฉํ•ด ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์ž๋ฃŒ๊ตฌ์กฐ์˜ ๊ฐ€๋ณ€์„ฑ ์—ฌ๋ถ€์— ๋”ฐ๋ผ hash() ๊ฐ’์ด ์ถ”์ถœ์ด ๋  ์ˆ˜ ์žˆ๊ณ  ์—†์„ ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋ถˆ๋ณ€ ๊ตฌ์กฐ์ธ ํŠœํ”Œ์€ hash ๊ฐ’์„ ๋ฝ‘์•„๋‚ผ ์ˆ˜ ์žˆ์ง€๋งŒ ๊ฐ€๋ณ€ ๊ตฌ์กฐ์ธ ๋ฆฌ์ŠคํŠธ๋Š” ๋ฝ‘์•„๋‚ผ ์ˆ˜ ์—†๋‹ค.

 

# Hash ๊ฐ’(uniqueํ•œ ๊ฐ’) ๋ฝ‘์•„๋‚ด๊ธฐ -> hash() ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ!
t1 = (10, 20, (30, 40, 50)) # tuple is immutable
t2 = (10, 20, [30, 40, 50]) # list is mutable
print('t1์˜ hash: ', hash(t1))
# print('t2์˜ hash: ', hash(t2)) -> list๋Š” mutableํ•ด์„œ ์–ธ์ œ๋“  ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ๋ฐ์ดํ„ฐ๊ฐ€ ๊ณ ์œ ์˜ ๊ฐ’์„ ๊ฐ–์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์—๋Ÿฌ๋ฐœ์ƒ

 

๋‹ค์Œ์€ ์ค‘๋ณต๋œ key๋ฅผ ๊ฐ–๋Š” ๋ฐ์ดํ„ฐ์—์„œ ํŠœํ”Œ๋กœ ์ฃผ์–ด์กŒ์„ ๋•Œ ์–ด๋–ป๊ฒŒ ํŽธ๋ฆฌํ•˜๊ฒŒ Dictionary๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋Š”์ง€ ์‚ดํŽด๋ณด์ž. ๋ฌผ๋ก  ์ผ๋ฐ˜์ ์ธ for๋ฌธ์„ ํ†ตํ•ด ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ setdefault ๋ฉ”์†Œ๋“œ๋ฅผ ํ™œ์šฉํ•˜๋ฉด ๋” ๊ฐ„๋‹จํ•œ ์ฝ”๋“œ๋กœ ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์ฐธ๊ณ ๋กœ ํŠœํ”Œ์— ์ค‘๋ณต๋œ ๊ฐ’์ด ์กด์žฌํ•  ๋•Œ,Dict comprehension์„ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋ถ€์ ์ ˆํ•˜๋‹ค. ์™œ๋ƒํ•˜๋ฉด Dict comprehension์€ ์ค‘๋ณต๋œ key์˜ ๋งˆ์ง€๋ง‰ ๊ฐ’๋“ค๋งŒ ์ฒ˜๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

# Dict์˜ setdefault ์˜ˆ์ œ for tuple to dictionary
source = (('k1', 'value1'),
        ('k1', 'value2'),
        ('k2', 'value3'),
        ('k2', 'value4'),
        ('k2', 'value5'))
new_dict1 = {}
new_dict2 = {}

# setdefault๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•์„ ์ด์šฉํ•ด Dictionary๋กœ ์ „ํ™˜!
for k, v in source:
    if k in new_dict1:
        new_dict1[k].append(v)
    else:
        new_dict1[k] = [v]
print("์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ• ์ด์šฉ:", new_dict1)

# setdefault๋ฅผ ์ด์šฉํ•ด ์ž๋™์ ์œผ๋กœ ์ค‘๋ณต๋˜๋Š” key๊ฐ’ ์ž˜ ์ฒ˜๋ฆฌํ•ด์ฃผ๊ธฐ
for k, v in source:
    new_dict2.setdefault(k, []).append(v)
print("setdefault ๋ฐฉ๋ฒ• ์ด์šฉ:", new_dict2)

# Dictionary comprehension์„ ์ด์šฉํ•˜๊ฒŒ ๋˜๋ฉด ์ค‘๋ณต๋˜๋Š” ๊ฐ’๋“ค ์ค‘ ๋งˆ์ง€๋ง‰ ๊ฐ’๋“ค๋งŒ ์ฒ˜๋ฆฌ๋จ
new_dict3 = {k: v for k, v in source}
print("Dict comprehension ์ด์šฉ:", new_dict3)

 

 

๊ฒฐ๊ณผ ํ™”๋ฉด

 

๋‹ค์Œ์€ ๋”•์…”๋„ˆ๋ฆฌ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ mutableํ•œ ๊ตฌ์กฐ์ด์ง€๋งŒ immutable ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•œ๋‹ค. MappingProxyType ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•ด์„œ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

# ์›๋ž˜ Dict๋Š” mutable ํ•˜์ง€๋งŒ Immutableํ•œ Dict๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Œ!
# ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ด์šฉ
from types import MappingProxyType

d = {'key1': 'value1'}
d_frozen = MappingProxyType(d)
print("d์™€ d_frozen์˜ ํ˜•ํƒœ๋Š” ๋™์ผํ•œ๊ฐ€?", d == d_frozen)
print("d์™€ d_frozen์˜ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ๋Š” ๋™์ผํ•œ๊ฐ€?", d is d_frozen)
print()

# ์ผ๋ฐ˜ dict ์ˆ˜์ •ํ•˜๊ธฐ
d['key2'] = 'value2'
print("์ˆ˜์ • ํ›„ d :", d)

# Immutable dict ์ˆ˜์ •ํ•ด๋ณด๊ธฐ
# d_frozen['key1'] = 'value2' => error ๋ฐœ์ƒ

 

๋งˆ์ง€๋ง‰์œผ๋กœ ์ง‘ํ•ฉํ˜• ์ž๋ฃŒ๊ตฌ์กฐ์ธ set์„ ์„ ์–ธํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ set comprehension์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž.

 

# Set ์„ ์–ธํ•˜๊ธฐ
s1 = {'apple', 'banana', 'kiwi', 'apple', 'banana'}
s2 = set(['apple', 'banana', 'kiwi', 'apple', 'banana'])
s3 = {'orange'}
s4 = set() # {}๋กœ ์„ ์–ธํ•˜๋ฉด set์ด ์•„๋‹Œ empty dict๋กœ ์ •์˜๋จ!
# set์— ๋ฐ์ดํ„ฐ ์ฒจ๊ฐ€
s1.add('orange')
s2.add('watermelon')
s2.update(['pear', 'grapes']) # ์—ฌ๋Ÿฌ๊ฐœ ์ฒจ๊ฐ€ํ• ๋• update!
s2.remove('kiwi') # ์‚ญ์ œํ• ๋• ์‚ญ์ œํ•  ๋ฐ์ดํ„ฐ ์ธ์ž๋กœ ๋„ฃ๊ธฐ
print(s1)
print(s2)
print(s3)
print(s4)

# Comprehension set => list comprehension๊ณผ ๋น„์Šท
result = {i for i in range(10)}
print(result, type(result))
print()

 

๋ฒˆ์™ธ๋กœ, ํŒŒ์ด์ฌ์€ bytes ์ฝ”๋“œ๋ฅผ ์ฝ๊ณ  ๋‚ด๋ถ€์ ์œผ๋กœ ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋ฅผ ์ˆ˜ํ–‰ํ•ด์„œ ์ฝ”๋“œ๋ฅผ runํ•˜๋Š”๋ฐ, ์ด ๋‚ด๋ถ€์ ์œผ๋กœ ๋Œ์•„๊ฐ€๋Š” ๊ณผ์ •์„ ๋ช‡ ๋‹จ๊ณ„๋กœ ๊ฑฐ์ณ์ง€๋Š”์ง€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•ด ๊ด€์ฐฐํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿผ์œผ๋กœ์จ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์„ ์–ธํ•  ๋•Œ ์ตœ์ ํ™”ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค. ๊ฐ„๋‹จํ•˜๊ฒŒ ์›์†Œ๊ฐ€ ํ•˜๋‚˜์ธ set ์ง‘ํ•ฉ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ 2๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ์„ ์–ธํ–ˆ์„ ๋•Œ, ์„ ์–ธ๋˜๋Š” ๋‚ด๋ถ€์ ์ธ ๊ตฌ์กฐ๋ฅผ ์‚ดํŽด๋ณด์ž.

 

# ์ž๋ฃŒ๊ตฌ์กฐ ์„ ์–ธ์‹œ ์ตœ์ ํ™” ํ•˜๊ธฐ
# ์–ด๋–ค ๋ฐฉ๋ฒ•์ด ์ตœ์ ํ™” ๋ฐฉ๋ฒ•์ธ์ง€ ์•Œ๊ธฐ ์œ„ํ•œ dis ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ •์˜
from dis import dis
# dis ๋ฉ”์†Œ๋“œ์•ˆ์— ๋ฌธ์ž์—ด ์ฒ˜๋Ÿผ ๋”ฐ์˜ดํ‘œ ์•ˆ์— ์ฝ”๋“œ๋ฅผ ๋„ฃ์–ด์ฃผ๊ธฐ!
print('-'*40)
print("์„ ์–ธ ๋ฐฉ๋ฒ•: 1", dis('{10}'))
print('-'*40)
print("์„ ์–ธ ๋ฐฉ๋ฒ•: 2", dis('set([10])'))

 

๊ฒฐ๊ณผ ํ™”๋ฉด

 

์œ„ ๊ฒฐ๊ณผํ™”๋ฉด์„ ๋ณด๋ฉด ๋‹จ์ˆœํžˆ {}๋ฅผ ์ด์šฉํ•ด ์„ ์–ธํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค set() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์„ ์–ธํ•˜๋Š” ๊ฒƒ์ด ๋‚ด๋ถ€์ ์œผ๋กœ ๋” ๋งŽ์€ ๋‹จ๊ณ„๋ฅผ ๊ฑฐ์นจ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ 2๊ฐ€์ง€ ๋ฐฉ๋ฒ• ์ค‘ ์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์ด ์ข€ ๋” ์ตœ์ ์˜ ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋ฐ˜์‘ํ˜•