Python에서 자료구조를 사용하는 법에 대한 강의였다.
학교에서 자료구조 수업 도우미를 1년간 했기에 자신있는 분야였다.
파이썬 Collections 내부에 처음보는 이름도 있어서 신기했다.
오늘 강의가 끝나면 Collections 파일을 뜯어볼 생각이다.
>>> a = [1, 2, 3, 4, 5]
>>> a.append(6) # Push
>>> a.pop() # Pop
6
>>> a.pop() # pop을 return 값도 가지며, 자료도 변화시킨다.
5
>>> a = [1, 2, 3, 4, 5]
>>> a.append(6) # enqueue
>>> a.pop(0) # dequeue
1
>>> a.pop(0)
2
>>> print(a)
[3, 4, 5, 6]
>>> t = (1, 2, 3)
>>> type(t)
tuple
>>> t + t
(1, 2, 3, 1, 2, 3)
>>> len(t)
3
>>> t[0] = 5 # 값을 변경할 수 없다.
TypeError: 'tuple' object does not support item assignment
>>> t2 = (2)
>>> type(t2)
int # 값이 하나인 tuple이여도, ','를 반드시 사용하자!
프로그램을 작동하는 동안 변경되지 않는 데이터를 저장할 때 주로 사용한다.
함수의 반환 값 등 사용자의 실수에 의한 에러를 사전에 방지할 수 있다.
>>> s = set([1, 2, 3, 1, 2, 3]) # s = {1, 2, 3} 도 가능하다.
>>> type(s)
set
>>> print(s)
{1, 2, 3} # 중복 자료는 제거한다.
>>> s.add(4) # 원소 추가
>>> s.remove(2) # 원소 제거. s.discard(2) 와 같은 문법
>>> print(s)
{1, 3, 4}
>>> s.update([1, 2, 5, 6]) # 한 번에 여러개 추가
>>> print(s)
{1, 2, 3, 4, 5, 6}
>>> s.clear() # 모든 원소를 삭제
remove()는 존재하지 않는 원소를 지우려고 하면 Error가 난다.
discard()는 원소가 존재하지 않는 경우도 보장시켜준다.
>>> s1 = {1, 2, 3, 4}
>>> s2 = {3, 4, 5, 6}
>>> s1.union(s2) # 합집합. s1 | s2 와 같은 문법이다.
{1, 2, 3, 4, 5, 6}
>>> s1.intersection(s2) # 교집합. s1 & s2 와 같은 문법이다.
{3, 4}
>>> s1.difference(s2) # 차집합. s1 - s2 와 같은 문법이다.
{1, 2}
>>> country_code = { "America":1, "Korea":82, "China":86 } # 선언. dict()로 생성 가능
>>> country_code.items() # 보통 for 문을 돌릴 떄 사용한다.
dict_items([('America', 1), ('Korea', 82), ('China', 86)]) # 각 tuple 형태이다.
>>> country_code.keys() # key 값들만 반환한다.
dict_keys(['America', 'Korea', 'China')]
>>> country_code['Japan'] = 81 # Dict 추가. 기존 Key에 value를 바꿀 수도 있다.
>>> country_code.values() # value 값들만 반환한다.
dict_values([1, 82, 86, 81])
>>> "Korea" in country_code.keys() # Key 값에 특정 값이 있는지 확인. value도 가능
>>> from collections import deque
>>> deque_list = deque() # 생성자
>>> for i in range(5):
... deque_list.append(i) # 뒤에 추가
>>> deque_list.appendleft(5) # 앞에 추가
>>> deque_list # extend([]), extendleft([]) 문법도 사용 가능하다.
deque([5, 0, 1, 2, 3, 4])
>>> deque_list.rotate(1)
>>> deque_list
deque([4, 5, 0, 1, 2, 3])
>>> deque_list.pop() # 뒤에서 제거
3
>>> deque_list.popleft() # 앞에서 제거
4
# insert, remove 등으로 인덱스에도 접근 가능하다.
Dict Type의 값을 key 또는 value로 정렬할 때 사용 가능하다.
>>> from collections import defaultdict
>>> d = defaultdict(labda : 0) # 초기값은 함수 형태로 넣어야된다.
>>> d["first"]
0
d = dict()
d["Key"]=d.get("Key",0) + 1 # get을 사용해서도 가능하다.
Sequence type의 data element들의 갯수를 dict 형태로 반환한다.
>>> from collections import Counter
>>> bat_count = ["B", "S", "B", "B", "S"]
>>> Counter(bat_count)
Counter({ 'B':3, 'S':2 })
파이썬 스타일의 코딩 기법과 파이썬 특유 문법을 활용해 효율적 코드 작성법을 알려주셨다.
고급 코드를 작성하기 위해서는 이러한 문제도 꼭 필요하다고 생각한다.
C++ 스타일에 익숙한 나에게 정말 유용한 강의였다.
text data를 핸들링하는 것에 대한 기초적인 문법이다.
>>> items = 'zero one two three'.split() # 빈칸을 기준으로 문자열 나누기
>>> print(items)
['zero', 'one', 'two', 'three']
>>> example = 'python,java,javascript'
>>> examples = example.split(',') # ','를 기준으로 문자열 나누기
>>> print(examples)
['python', 'java', 'javascript']
>>> p, j, js = example.split(',') # 패킹, 언패킹도 가능
>>> '-'.join(examples) # join 문법. 사이에 '-'를 추가해서 문자열로 반환
'python-java-javascript'
>>> result = [i for i in range(5)] # 기본 문법(for range)
>>> result
[0, 1, 2, 3, 4]
>>> result = [i for i in range(10) if i % 2 == 0] # if문으로 filter 추가 가능
>>> result
[0, 2, 4, 6, 8]
>>> word1 = 'ab'
>>> word2 = 'cd'
>>> result = [i + j for i in word1 for j in word2] # 2중 for문도 가능
>>> result
['ac', 'ad', 'bc', 'bd']
>>> result = [ [i + j for i in word1] for j in word2] # 뒤의 for문이 먼저 동작
>>> result
[['ac, bc'], ['ad, 'bd']]
list의 element를 추출할 때 번호를 붙여서 추출해준다.
>>> for i, v in enumerate('ABC'): # 기본 문법(list도 가능!)
... print(i, v)
0 A
1 B
2 C
>>> {v : i for i, v in enumerate('ABCD')} # dictionary로 만들기
{'A' : 0, 'B' : 1, 'C' : 2, 'D' : 3}
두 개의 list의 값을 병렬적으로 추출한다.
>>> alist = ['a1', 'a2', 'a3']
>>> blist = ['b1', 'b2', 'b3']
>>> for a, b in zip(alist, blist): # 병렬적으로 값을 추출
... print(a, b)
a1 b1
a2 b2
a3 c3
함수 이름 없이, 함수처럼 쓸 수 있는 익명 함수이다.
수학의 람다 대수에서 유래했다.
"""
def f(x, y):
return x + y
"""
>>> f = lambda x, y : x + y # 기몬 문법. 상단 함수와 동일 로직
>>> f(10, 20)
30
sequence형 data가 있을 때 각각의 data를 mapping을 해주는 function이다.
>>> ex = [1, 2, 3, 4, 5]
>>> list(map(lambda x : x ** 2, ex)) # list화 해주어야 됨!
[1, 4, 9, 16, 25]
list comprehension, lambda 등으로 대체가 가능하다.
map function과 달리 list에 똑같은 함수를 적용해서 통합한다.
>>> from functools import reduce
>>> print(reduce(lambda x, y : x + y, [1, 2, 3, 4, 5])) # 하단 사진 참고
15
👉 Map-Reduce는 대용량 데이터를 다루는 Legacy library나 다양한 ML 코드에서 주로 사용한다.
Sequence 자료형에서 데이터를 순서대로 추출하는 object
>>> cities = ['Seoul', 'Busan', 'Suwon']
>>> memory_address = iter(cities) # iterable objects 선언
>>> next(memory_address)
'Seoul'
>>> next(memory_address)
'Busan'
>>> next(memory_address)
'Suwon'
>>> next(memory_address)
StopIteration
>>> gen_ex = (n * n for n in range(100))
>>> print(type(gen_ex)) # 호출 단계에서 하나씩 반환된다.
<class 'generator'>
메모리를 아끼기 위해서 대용량 데이터 처리에 사용하면 좋다.
함수에 입력되는 arguments의 다양한 형태이다.
def print_person(name, age):
print(name, age)
print_person("Gyuho", 26)
print_person(name="Gyuho", age="26") # <<<
def print_person(name, age=20):
print(name, age)
print_person("Gyuho") # age는 자동으로 20이 들어간다.
print_person("Gyuho", 26) # 물론, 값을 넣을수도 있다.
def asterisk_test(*args):
return sum(args)
print(asterisk_test(1, 2, 3, 4, 5))
# 15가 출력된다.
def kwargs_test(**kwargs):
print(kwargs)
kwargs_test(first = 1, second = 2, third = 3)
# {'first':1, 'second:'2, 'third':3} 가 출력된다.
⭐️ 이 밖에도 asterisk는 tuple, dic 등 자료형의 값을 unpacking 할 때도 사용된다.