primitive type이 아닌, 어떤 특징이 있는 data는 어떻게 저장하면 좋을까?
예를들면) 전화번호부, 은행번호표, 서적정보, 택배 상하차 data 등
a = [1, 2, 3, 4, 5]
a.append(10)
a.append(20)
a.pop()
#20
a.pop()
#10
a = [1, 2, 3, 4, 5]
a.append(10)
a.append(20)
a.pop(0)
#1
a.pop(0)
#2
t = (1, 2, 3)
print(t + t, t * 2)
#(1, 2, 3, 1, 2, 3) (1, 2, 3, 1, 2, 3)
len(t)
#3
t[1] = 5 # -> Error 발생
t = (1)
#1
t = (1, )
#(1, )
s1 = set([1, 2, 3, 4, 5])
s2 = set([3, 4, 5, 6, 7])
s1.union(s2)
#{1, 2, 3, 4, 5, 6, 7}
s1 | s2
#{1, 2, 3, 4, 5, 6, 7}
s1.intersection(s2)
#{3, 4, 5}
s1 & s2
#{3, 4, 5}
s1.difference(s2)
#{1, 2}
s1 - s2
#{1, 2}
country_code = {}
country_code = {"America" : 1, "Korea" : 82, "China" : 86, "Japan" : 81}
country_code.items() #키랑 값 둘 다
#Dict_items([("America", 1), ("China", 86), ("Korea", 82), ("Japan", 81)])
#country_code.keys() #키 값만
#Dict_keys(["America", "China", "Korea", "Japan"])
country_code["German"] = 49 #dict에 추가
country_code.values() # value값들
#dict_values([1, 49, 86, 82, 81])

>>> from collections import deque
>>> deque_list = deque()
>>> for i in range(5) :
... deque_list.append(i)
...
>>> deque_list.append(10)
>>> deque_list.appendleft(20)
>>> deque_list
deque([20, 0, 1, 2, 3, 4, 10])
>>> deque_list.rotate(2)
>>> deque_list
deque([4, 10, 20, 0, 1, 2, 3])
>>> deque_list = deque(reversed(deque_list))
>>> deque_list
deque([3, 2, 1, 0, 20, 10, 4])
>>> deque_list.extend([5, 6, 7])
>>> deque_list
deque([3, 2, 1, 0, 20, 10, 4, 5, 6, 7])
>>> deque_list.extendleft([5, 6, 7])
>>> deque_list
deque([7, 6, 5, 3, 2, 1, 0, 20, 10, 4, 5, 6, 7])
from collections import deque
import time
start_time = time.clock()
deque_list = deque()
# Stack
for i in range(10000):
for i in range(10000):
deque_list.append(i)
deque_list.pop()
print(time.clock() - start_time, "seconds")
import time
start_time = time.clock()
just_list = []
for i in range(10000):
for i in range(10000):
just_list.append(i)
just_list.pop()
print(time.clock() - start_time, "seconds")
from collections import OrderedDict
d = OrderedDict()
d['x'] = 100
d['y'] = 200
d['z'] = 300
d['l'] = 500
for k, v in OrderedDict(sorted(d.items(), key = lambda t: t[0])).items() :
print(k, v)
for k, v in OrederedDict(sortedd.items(), key = lambda t: t[1])).items() :
print(k, v)
from collections import defaultdict
from collections import OrderedDict
word_count = defaultdict(lambda : 0)
for word in text :
word_count[word] += 1
for i, v in OrderedDict(sorted(word_count.items(), key = lambda t: t[1], reverse = True)).items() :
print(i, v)
from collections import Counter
c = Counter()
c = Counter('gallahad')
print(c) #Counter({'a' : 3, 'l' : 2, 'g' : 1, 'd' : 1, 'h' : 1})
c = Counter({'red' : 4, 'blue' : 2})
print(list(c.elements())) #['blue', 'blue', 'red', 'red', 'red', 'red']
c = Counter(cats = 4, dogs = 8)
print(list(c.elements())) #['dogs', 'dogs', 'dogs' ,,, 'cats', 'cats', cats']
c = Counter(a = 4, b = 2, c = 0, d = -2)
d = Counter(a = 1, b = 2, c = 3, d = 4)
print(c + d) #Counter({'a' : 5, 'b' : 4, 'c' : 3, 'd' : 2})
print(c & d) #Counter({'b' : 2, 'a' : 1})
print(c | d) #Counter({'a' : 4, 'd' : 4, 'c' : 3, 'b' : 2})
#wordcounter
text = """A press release is the quickest and easiest way to get free publicity. If well written, a press release can result in multiple published articles about your firm and its products. And that can mean new prospects contacting you asking you to sell to them. ….""".lower().split()
print(Counter(text))
#Counter({'a' : 12, 'to' : 10, 'the' : 9, 'and' : 9,,,})
print(Counter(text)["a"])
#12
>>> from collections import namedtuple
>>> Point = namedtuple("Point", ['x', 'y'])
>>> p = Point(11, y = 22)
>>> print(p[0] + p[1])
33
>>> x, y = p
>>> print(x, y)
11 22
>>> print(p.x + p.y)
33
>>> print(Point(x = 11, y = 22))
Point(x=11, y=22)
Python Style coding 기법 (파이썬에서 주로 많이 쓰는)
파이썬 특유의 문법을 활용하여 효율적으로 코드를 표현함.
그러나 더 이상 파이썬 특유는 아님, 많은 언어들이 서로의 장점을 채용
고급 코드를 작성할 수록 더 많이 필요함.
예시)
colors = ['red', 'blue', 'green', 'yellow']
result = ''
for s in colors :
result = result + s
colors = ['red', 'blue', 'green', 'yellow']
result = ''.join(colors)
result
#'redbluegreenyellow'