The Not-So-Basics
Sorting
x = [4,1,2,3]
y = sorted(x)
x.sort()
x = sorted ([-4 ,1 ,-2 ,3 ], key=abs , reverse=True )
wc = sorted (word_counts.items(),
key=lambda x: x[1 ],
reverse=True )
wc
x ๋ฆฌ์คํธ๋ ์ ๋๊ฐ์ ๋ฐ๋ผ ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌ ๋ฉ๋๋ค.
sorted() ํจ์๋ ์
๋ ฅ ๋ฆฌ์คํธ๋ฅผ ์ ๋ ฌํฉ๋๋ค.
key=abs ์ธ์๋ ์ ๋ ฌ ํค๋ฅผ ์ ๋๊ฐ์ผ๋ก ์ค์ ํฉ๋๋ค.
reverse=True ์ธ์๋ ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌ๋จ์ ๋ํ๋
๋๋ค.
wc๋ ๋จ์ด ๋ฐ ํด๋น ์นด์ดํธ๋ฅผ ๊ฐ์ง ๋์
๋๋ฆฌ๋ฅผ ๊ฐ์ฅ ๋ง์ ์นด์ดํธ๋ถํฐ ๊ฐ์ฅ ์ ์ ์นด์ดํธ๋ก ์ ๋ ฌ ๋ฉ๋๋ค.
sorted() ํจ์์ key ๋งค๊ฐ๋ณ์๋ ๊ฐ ์์์ ๋ํด ์ ์ฉ๋๋ ํจ์๋ฅผ ์ง์ ํฉ๋๋ค.
์ฌ๊ธฐ์๋ ๋๋ค ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ์์์ ๋ ๋ฒ์งธ ๊ฐ(์นด์ดํธ), ๋จ์ด์ ์ถ์ฐ ๋น๋๋ฅผ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํฉ๋๋ค.
reverse=True ์ธ์๋ ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌ๋จ์ ๋ํ๋
๋๋ค.
word_counts.items()๋ ๋์
๋๋ฆฌ์ ๊ฐ ํญ๋ชฉ์ ํํ๋ก ๋ฐํ ํฉ๋๋ค. ์๋ฅผ ๋ค์ด, { 'I': 2, 'am': 1, 'a': 1 }์ด๋ผ๋ ๋์
๋๋ฆฌ๊ฐ ์๋ค๋ฉด, items() ๋ฉ์๋๋ ('I', 2), ('am', 1), ('a', 1)๊ณผ ๊ฐ์ ํํ๋ค์ ๋ฆฌ์คํธ๋ฅผ ๋ฐํํฉ๋๋ค.sorted() ํจ์์ ์ํด ๋ ๋ฒ์งธ ์์(์ฆ, ํํ์ ๋ ๋ฒ์งธ ๊ฐ)๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌ ๋ฉ๋๋ค. ๋ฐ๋ผ์ word_counts.items()๊ฐ ํํ๋ค์ ๋ฆฌ์คํธ๋ฅผ ๋ฐํํ๋ฏ๋ก ๋ ๋ฒ์งธ ๊ฐ์ ํด๋น ๋จ์ด์ ์ถํ ๋น๋๋ฅผ ๋ํ๋
๋๋ค. x[0]: โ๋จ์ดโ, x[1]: โ์ซ์โ
๋จ์ด์ ํด๋น ์นด์ดํธ๋ฅผ ๊ฐ์ฅ ๋ง์ ์นด์ดํธ๋ถํฐ ๊ฐ์ฅ ์ ์ ์นด์ดํธ๋ก ์ ๋ ฌ ํฉ๋๋ค.
List Comprehensions
ํน์ ์์๋ง์ ์ ํํ๊ฑฐ๋ ์์๋ฅผ ๋ณํํ๊ฑฐ๋ ๋ ๋ค๋ฅผ ์ ํํ์ฌ ๋ชฉ๋ก์ ๋ค๋ฅธ ๋ชฉ๋ก์ผ๋ก ๋ณํํ๊ณ ์ ํ ๊ฒ ์
๋๋ค.
๋ชฉ๋ก์ ํฌ๊ด์ฑ์ ํ์ดํ ๋ ๋ฐฉ์์ ์ํด ๊ฒฐ์ ๋ฉ๋๋ค.
๊ฐ๋ฅํ๋ฉด ํญ์ List Comprehensions๋ฅผ ์ฌ์ฉํฉ๋๋ค.
even_numbers = [x for x in range(5 ) if x % 2 == 0 ]
squares = [x * x for x in range(5 )]
even_squares = [x * x for x in even_numbers]
๋ง์ฐฌ๊ฐ์ง๋ก List์ Dictionary ๋๋ Set์ผ๋ก ๋ณํ ํ ์ ์์ต๋๋ค.
square_dict = { x : x * x for x in range(5 ) } # { 0 :0 , 1 :1 , 2 :4 , 3 :9 , 4 :16 }
square_set = { x * x for x in [1, -1] } # { 1 } # set์ ์ค๋ณต ์์ ํฌํจ X
๋ณ์๋ก ๋ฐ์ค์ ์ฌ์ฉํ๋ ๊ฒ์ด ์ผ๋ฐ ์ ์
๋๋ค.
โ_โ์ ๊ฐ์ด ํ์ํ์ง ์์๋ ์ฌ์ฉ. even_numbers = [0, 2, 4] โ Zeros List: [0, 0, 0]'
zeroes = [0 for _ in even_numbers]
List Comprehension ๋ค์๊ณผ ๊ฐ์ ์ฌ๋ฌ ๊ฐ์ง ์ด์ ๊ฐ ํฌํจ ๋ ์ ์์ต๋๋ค.
pairs = [(x, y)
for x in range(10 )
for y in range(10 )]
์ปดํจํฐ ์ฌ์ฉ์ด ์ฌ์ด ๊ฑฐ๋ฆฌ ํ๋ ฌ, ๋์ค์๋ ์ด์ ์ ๊ฒฐ๊ณผ๋ฅผ ์ฌ์ฉ ํ ์ ์์ต๋๋ค.
increasing_pairs = [(x, y)
for x in range(10 )
for y in range(x + 1 , 10 )]
0๋ถํฐ 9๊น์ง์ธ ๋ชจ๋ ์ซ์ ์์ ์์ฑ ํฉ๋๋ค.
์ด ์๋ค์ ์ฒซ ๋ฒ์งธ ์์๊ฐ ๋ ๋ฒ์งธ ์์๋ณด๋ค ์์ ์ฆ๊ฐํ๋ ์์์์
๋๋ค.
Generators & Iterators
List์ ๋ฌธ์ ์ ์ List๊ฐ ์ฝ๊ฒ ๋งค์ฐ ์ปค์ง ์ ์๋ค๋ ๊ฒ ์
๋๋ค.
range(1000000)๋ ์ค์ 100๋ง ๊ฐ์ ์์ ๋ชฉ๋ก์ ๋ง๋ญ๋๋ค.
๋ง์ฝ ๋น์ ์ด ํ ๋ฒ์ ํ๋์ฉ๋ง ๋ค๋ฃจ๋ฉด ๋๋ค๋ฉด, ์ด๊ฒ์ ์์ฒญ๋ ๋นํจ์จ์ฑ์ ์์ฒ(๋๋ ๋ฉ๋ชจ๋ฆฌ ๋ถ์กฑ์ ์์ธ)์ด ๋ ์ ์์ต๋๋ค .
๋ง์ฝ ๋น์ ์ด ์ ์ฌ์ ์ผ๋ก ์ฒ์ ๋ช ๊ฐ์ ๊ฐ๋ง ํ์ํ๋ค๋ฉด, ๊ทธ๊ฒ๋ค์ ๋ชจ๋ ๊ณ์ฐํ๋ ๊ฒ์ ๋ญ๋น์
๋๋ค.
Generator๋ ์ฌ์ฉ์๊ฐ ๋ฐ๋ณตํ ์ ์์ง๋ง(์ฐ๋ฆฌ์ ๊ฒฝ์ฐ, ์ผ๋ฐ์ ์ผ๋ก ์ ์ฌ์ฉํฉ๋๋ค) ํ์์ ๋ฐ๋ผ ๊ฐ์ด ์์ฑ๋๋ ๊ฒ( lazily)์
๋๋ค.
Generator๋ฅผ ๋ง๋๋ ํ ๊ฐ์ง ๋ฐฉ๋ฒ์ ํจ์์ yield ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์
๋๋ค:
def lazy_range (n ):
"""a lazy version of range"""
i = 0
while i < n:
yield i
i += 1
for i in lazy_range(10 ):
print (i)
0
1
2
3
4
5
6
7
8
9
for i in lazy_range(10000):
if i == 3: break
print (i)
0
1
2
t = lazy_range(3 )
next (t)
next (t)
next (t)
def lazy_inf_range () :
i = 0
while True:
yield i
i += 1
t = lazy_inf_range()
next (t)
next (t)
next (t)
Generator๋ฅผ ๋ง๋๋ ๋ ๋ฒ์งธ ๋ฐฉ๋ฒ์ ๊ดํธ ์์ผ๋ก ํฌ์ฅ๋ comprehension์ ์ฌ์ฉํ๋ ๊ฒ์
๋๋ค:
lazy_evens_below_20 = (i for i in lazy_range(20 ) if i % 2 == 0 )
lazy_evens_below_20
Randomness
๋์๋ฅผ ์์ฑํ๊ธฐ ์ํด, ์ฐ๋ฆฌ๋ ๋์ ๋ชจ๋์ ์ฌ์ฉํ ์ ์์ต๋๋ค .
random.random()๋ 0์์ 1 ์ฌ์ด์ ์ซ์๋ฅผ ๊ท ์ผํ๊ฒ ์์ฑํฉ๋๋ค.
import random
four_uniform_randoms = [random .random () for _ in range(4 )]
four_uniform_randoms
[0.15001730378211198 ,
0.047689363188983425 ,
0.4438845111618783 ,
0.8064273339306516 ]
reproducible ๊ฐ๋ฅํ ๊ฒฐ๊ณผ๋ฅผ ์ป์ผ๋ ค๋ ๊ฒฝ์ฐ.
random .seed(10 )
print (random .random ())
random .seed(10 )
print (random .random ())
0 .5714025946899135
0 .5714025946899135
random.randrange๋ 1 ๋๋ 2๊ฐ์ ์ธ์๋ฅผ ์ฌ์ฉํ๊ณ ํด๋น ๋ฒ์ ()์์ ์์๋ก ์ ํํ ์์๋ฅผ ๋ฐํํฉ๋๋ค.
random .randrange(10 ) # choose randomly from range(10 ) = [0, 1, ..., 9]
random .randrange(3 , 6 ) # choose randomly from range(3 , 6 ) = [3, 4, 5]
random.shuffle๋ ๋ชฉ๋ก์ ์์๋ฅผ ์์๋ก ์ฌ์ ๋ ฌํฉ๋๋ค:
up_to_ten = list (range (10 ))
random.shuffle(up_to_ten)
print (up_to_ten)
๋ชฉ๋ก์์ ํ ์์๋ฅผ ์์๋ก ์ ํํ๋ ๋ฐฉ๋ฒ์
๋๋ค.
my_best_friend = random.choice(["Alice" , "Bob" , "Charlie" ])
๋์ฒดํ์ง ์๊ณ ์์ ํ๋ณธ์ ์์๋ก ์ ํํ๋ ๋ฐฉ๋ฒ์
๋๋ค. (์ฆ, ์ค๋ณต๋์ง ์์ต๋๋ค)
lottery_numbers = range(60 )
winning_numbers = random.sample(lottery_numbers, 6 )
๋์ฒด ์์ ํ๋ณธ์ ์ ํํ๋ ๋ฐฉ๋ฒ์
๋๋ค.(์ฆ, ์ค๋ณต์ ํ์ฉํฉ๋๋ค)
four_with_replacement = [random.choice(range (10 )) for _ in range (4 )]
four_with_replacement
random.choice(range(10))๋ 0๋ถํฐ 9๊น์ง์ ์ซ์ ์ค์์ ํ๋๋ฅผ ๋ฌด์์๋ก ์ ํํ๋ ํจ์์
๋๋ค.
์ด๋ฅผ ๋ฆฌ์คํธ ์ปดํ๋ฆฌํจ์
์ผ๋ก 4๋ฒ ๋ฐ๋ณตํ์ฌ ๋ฆฌ์คํธ๋ฅผ ์์ฑ. ์ค๋ณต์ ํ์ฉํฉ๋๋ค. ๋ฌด์์๋ก ์ ํ๋ 0๋ถํฐ 9๊น์ง์ ์ซ์๊ฐ 4๊ฐ ํฌํจ.
Regular Expressions
์ ๊ท ํํ์์ ํ
์คํธ๋ฅผ ๊ฒ์ํ๋ ๋ฐฉ๋ฒ์ ์ ๊ณต ํฉ๋๋ค.
๊ทธ๊ฒ๋ค์ ๋ฏฟ์ ์ ์์ ์ ๋๋ก ์ ์ฉํ์ง๋ง ๋ํ ๊ฝค ๋ณต์กํด์ ๊ทธ๊ฒ๋ค์ ๋ํ ์ ์ฒด ์ฑ
์ด ์์ต๋๋ค.
import re
# ์ ๊ท ํํ์์ ์ฌ์ฉํ์ฌ ๋ค์ํ ๋ฌธ์์ด ์์
์ ์ํํ๊ณ , ๊ฐ ์์
์ ๊ฒฐ๊ณผ๋ฅผ ๊ฒ์ฌํฉ๋๋ค.
# ๋ชจ๋ ์กฐ๊ฑด์ด ์ฐธ์ผ ๊ฒฝ์ฐ True๋ฅผ ์ถ๋ ฅํฉ๋๋ค.
print (all([
not re.match ("a" , "cat" ), # "cat" ๋ฌธ์์ด์ "a" ๋ก ์์ํ์ง ์์
re.search("a" , "cat" ), # "cat" ๋ฌธ์์ด์ "a" ๊ฐ ํฌํจ๋์ด ์์
not re.search("c" , "dog" ), # "dog" ๋ฌธ์์ด์ "c" ๊ฐ ํฌํจ๋์ด ์์ง ์์
3 == len (re.split("[ab]" , "carbs" )), # "carbs" ๋ฌธ์์ด์ "[ab]" ๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ถํ ํ๋ฉด ['c' , 'r' , 's' ]๊ฐ ๋๋ฉฐ, ๊ธธ์ด๊ฐ 3 ์
"R-D-" == re.sub ("[0-9]" , "-" , "R2D2" ) # "R2D2" ๋ฌธ์์ด์์ ์ซ์๋ฅผ "-" ๋ก ๋์ฒดํ๋ฉด "R-D-" ๊ฐ ๋จ
])) # ์ถ๋ ฅ ๊ฒฐ๊ณผ๋ True
Object-Oriented Programming
class Set :
def __init__ (self, values=None ):
"""์ด๊ฒ์ ์์ฑ์์
๋๋ค.
์๋ก์ด Set์ ๋ง๋ค ๋ ํธ์ถ๋ฉ๋๋ค.
๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ ์ ์์ต๋๋ค.
s1 = Set() # ๋น ์งํฉ
s2 = Set([1,2,2,3]) # ๊ฐ์ผ๋ก ์ด๊ธฐํ"""
self.dict = {}
if values is not None :
for value in values:
self.add(value)
def __repr__ (self ):
"""์ด๊ฒ์ Set ๊ฐ์ฒด์ ๋ฌธ์์ด ํํ์
๋๋ค.
Python ํ๋กฌํํธ์์ ์
๋ ฅํ๊ฑฐ๋ str()์ ์ ๋ฌํ๋ฉด ์ฌ์ฉ๋ฉ๋๋ค."""
return "Set: " + str (self.dict .keys())
def add (self, value ):
self.dict [value] = True
def contains (self, value ):
return value in self.dict
def remove (self, value ):
del self.dict [value]
s = Set([1 ,2 ,3 ])
s .add(4 )
print (s.contains(4 )) # True
s .remove(3 )
print (s.contains(3 )) # False
Function Tools
ํจ์๋ฅผ ์ ๋ฌํ ๋ ์ ํจ์๋ฅผ ๋ง๋ค๊ธฐ ์ํด ๋ถ๋ถ์ ์ผ๋ก (๋๋ ์นด๋ ) ํจ์๋ฅผ ์ ์ฉํ๊ณ ์ถ์ ๋๊ฐ ์์ต๋๋ค
def exp (base, power) :
return base ** power
def two_to_the (power) :
return exp(2 , power)
two_to_the(3 )
functools.partial์ ์ฌ์ฉํ๋ ๊ฒ์ ๋ค๋ฅธ ์ ๊ทผ ๋ฐฉ์
from functools import partial
two_to_the = partial(exp, 2 )
print (two_to_the(3 ))
square_of = partial(exp, power=2 )
print (square_of(3 ))
๋ํ map, reduce ๋ฐ filter๋ฅผ ์ฌ์ฉํ์ฌ ์ดํด๋ฅผ ๋์ดํ๋ ๊ธฐ๋ฅ์ ๋์์ ์ ๊ณตํ๊ธฐ๋ ํฉ๋๋ค:
ํญ์ map ์ฌ์ฉํ๊ณ ๊ฐ๋ฅํ๋ฉด reduceํ๊ณ filteringํฉ๋๋ค.
Map
def double(x):
return 2 * x
xs = [1, 2, 3, 4]
twice_xs = [double(x) for x in xs]
twice_xs = map(double, xs)
list_doubler = partial(map, double)
twice_xs = list_doubler(xs)
def multiply (x, y ): return x * y
products = map (multiply, [1 , 2 ], [4 , 5 ])
list (products)
def multiply (x, y, z ): return x * y * z
products = map (multiply, [1 , 2 ], [4 , 5 ], [10 , 20 ])
list (products)
Filter
def is_even (x ):
"""x๊ฐ ์ง์์ด๋ฉด True, ํ์์ด๋ฉด False๋ฅผ ๋ฐํํฉ๋๋ค."""
return x % 2 == 0
xs = [1 , 2 , 3 , 4 ]
x_evens = [x for x in xs if is_even(x)]
print (x_evens)
x_evens = filter (is_even, xs)
print (list (x_evens))
list_evener = partial(filter , is_even)
x_evens = list_evener(xs)
print (list (x_evens))
[2 , 4 ]
[2 , 4 ]
Reduce
def multiply(x, y): return x * y
xs = [1,2,3]
x_product = reduce(multiply, xs)
print(x_product)
list_product = partial(reduce, multiply)
x_product = list_product(xs)
print(x_product)
6
6
Enumerate
๋ชฉ๋ก์์ ๋ฐ๋ณตํ๊ณ ํด๋น ์์์ ์ธ๋ฑ์ค๋ฅผ ๋ชจ๋ ์ฌ์ฉํ๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ํ๋ฉด ๋ฉ๋๋ค.
documents = ["I" , "am" , "a" , "boy" ]
for i in range(len(documents)):
document = documents[i]
print (i, document )
i = 0
for document in documents:
print (i, document )
i += 1
0 I
1 am
2 a
3 boy
0 I
1 am
2 a
3 boy
Pythonic solution์ ์ด๊ฑฐํ์ผ๋ก ํํ(์ธ๋ฑ์ค, ์์)์ ์์ฑํฉ๋๋ค:
for i, document in enumerate(documents):
print (i, document )
0 I
1 am
2 a
3 boy
for i in range(len(documents)): print (i)
for i, _ in enumerate(documents): print (i)
0
1
2
3
0
1
2
3
Zip & Unzip
๋ ์ด์์ ๋ชฉ๋ก์ ํจ๊ป ์์ถ ํฉ๋๋ค.
zip์ ์ฌ๋ฌ ๋ชฉ๋ก์ ํด๋น ์์์ ํํ ๋จ์ผ ๋ชฉ๋ก์ผ๋ก ๋ณํํฉ๋๋ค:
list1 = ['a' , 'b' , 'c' ]
list2 = [1 , 2 , 3 ]
list (zip (list1, list2)) # is [('a' , 1), ('b' , 2), ('c' , 3)]
์ด์ํ ์์์๋ฅผ ์ฌ์ฉํ์ฌ List๋ฅผ "Unzip"ํ ์๋ ์์ต๋๋ค:
pairs = [('a' , 1 ), ('b' , 2 ), ('c' , 3 )]
letters, numbers = zip(*pairs )
print (letters, numbers)
# ('a' , 'b' , 'c' ) (1 , 2 , 3 )
pairs = [('a' , 1 ), ('b' , 2 ), ('c' , 3 )]
letters, numbers = zip(('a' , 1 ), ('b' , 2 ), ('c' , 3 ))
print (letters, numbers)
# ('a' , 'b' , 'c' ) (1 , 2 , 3 )