네이버 부스트코스 '머신러닝을 위한 파이썬' 강의 내용 정리
from collections import deque
from collections import Counter
from collections import OrderedDict
from collections import defaultdict
from collections import namedtuple
append
, pop
, appendleft
, popleft
import time
start_time = time.clock()
just_list = []
for i in range(10000):
for j in range(10000):
just_list.append(i)
just_list.pop()
print(time.clock() - start_time, "seconds")
>>> 26.9465943 seconds
from collections import deque
import time
start_time = time.clock()
just_list = deque()
for i in range(10000):
for j in range(10000):
just_list.append(i)
just_list.pop()
print(time.clock() - start_time, "seconds")
>>> 9.8335841 seconds
from collections import OrderedDict
d = OrderedDict()
d['x'] = 100
d['y'] = 200
d['z'] = 300
d['l'] = 500
for k, v in d.items():
print(k, v)
>>> x 100
>>> y 200
>>> z 300
>>> l 500
for k,v in OrderedDict(sorted(d.items(), key = lambda t: t[0])).items():
print(k,v)
>>> l 500
>>> x 100
>>> y 200
>>> z 300
for k,v in OrderedDict(sorted(d.items(), key = lambda t: t[1])).items():
print(k,v)
>>> x 100
>>> y 200
>>> z 300
>>> l 500
d = dict()
print(d["first"])
>>> keyError
from collections import defaultdict
d = defaultdict(object) # defaultdict 생성
d = defaultdict(lambda : 0) # 기본값을 0으로 설정
print(d["first"])
>>> 0
from collections import Counter
c = Counter()
c = Counter('gallanhad')
print(c) # 정렬까지해서 나타남
>>> Counter({'a': 3, 'l': 2, 'g': 1, 'n': 1, 'h': 1, 'd': 1})
from collections import namedtuple
Point = namedtuple('Point', ['x','y'])
p = Point(11, y = 22)
print(p[0] + p[1])
x, y = p
print(x, y)
print(p.x + p.y)
print(Point(x=11, y=22))
>>> 33
>>> 11 22
>>> 33
>>> Point(x=11, y=22)