-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path12_collection.py
201 lines (156 loc) ยท 5.65 KB
/
12_collection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from collections import defaultdict
#------------------
# 1. defaultdict
#------------------
# dict์ ๋ฌ๋ฆฌ defaultdict๋ฅผ ์ฌ์ฉํ๋ฉด key๊ฐ์ ์กด์ฌ ์ ๋ฌด๋ฅผ ํ์ธํ ํ์๊ฐ ์๋ค.
colors = (
('A', 'yellow'),
('B', 'blue'),
('C', 'green'),
('B', 'black'),
('A', 'red'),
('D', 'silver'))
print(type(colors)) # tuple
favoriteColor = defaultdict(list)
for name, color in colors:
favoriteColor[name].append(color)
print(favoriteColor)
# defaultdict(<class 'list'>,
# {'A': ['yellow', 'red'],
# 'B': ['blue', 'black'],
# 'C': ['green'],
# 'D': ['silver']})
# ๋๋ค๋ฅธ ์ค์ํ ์ฌ์ฉ๋ฐฉ๋ฒ์ dict ์์ nested list(๋ฆฌ์คํธ ์์ ๋ฆฌ์คํธ๊ฐ ์๋ ํํ)๋ฅผ ์ถ๊ฐํ๋ ๊ฒ.
# ํค ๊ฐ์ด ์ฌ์ ์์ ์กด์ฌํ์ง ์๋๋ค๋ฉด, KeyError๊ฐ ๋ฐ์ํ ๊ฒ์ด๋ค.
# defaultdict๋ ์ด๋ฐ ๋ฌธ์ ๋ฅผ ์ฐฝ์์ ์ผ๋ก ํด๊ฒฐํ๋ค.
# KeyError๊ฐ ๋จ๋ ์์๋ฅผ ๋ณด๊ณ ๋์ defaultdict ์ฌ์ฉ๋ฒ์ ์์๋ณด์
# Problem
#some_dict = {}
#some_dict['colors']['favorite'] = 'yellow' # KeyError: 'colors'
# Solution
import collections
tree = lambda: collections.defaultdict(tree)
some_dict = tree()
some_dict['colors']['favorite'] = 'yellow'
# json.dumps๋ฅผ ์ฌ์ฉํด์ some_dict๋ฅผ ์ถ๋ ฅํ ์๋ ์๋ค.
import json
print(json.dumps(some_dict)) # {"colors": {"favorite": "yellow"}}
#------------------
# 2. orderedDict
#------------------
# orderedDict๋ ์ฒ์ ์ฝ์
๋ ๋์ ์์๋๋ก ํญ๋ชฉ์ ์ ๋ ฌ๋ ์ํ๋ก ์ ์งํ๋ค.
# ๊ธฐ์กด ํค์ ๊ฐ์ ๋ฎ์ด ์ฐ๋๋ผ๋ ํด๋น ํค์ ์์น๋ ๋ณ๊ฒฝ๋์ง ์๋๋ค.
# Problem
colors = {"red": 198, "green": 170, "blue": 160}
for key, value in colors.items():
print(key, value)
# random ํ ์์๋ก ์ถ๋ ฅ๋จ.
# Solution
from collections import OrderedDict
colors = OrderedDict( [("red", 198), ("green", 170), ("blue", 160)] )
for key, value in colors.items():
print(key, value)
#------------------
# 3. counter
#------------------
# Counter๋ ํน์ ์์ดํ
์ ๊ฐ์๋ฅผ ์ธ๋ ํจ์์ด๋ค.
from collections import defaultdict
from collections import Counter
colors = (
('A', 'yellow'),
('B', 'blue'),
('C', 'green'),
('B', 'black'),
('A', 'red'),
('D', 'silver'))
favs = Counter(name for name, color in colors)
print(favs) # Counter({'A': 2, 'B': 2, 'C': 1, 'D': 1}
# ํ์ผ ์์์ ๊ฐ์ฅ ๋ง์ด ๋ฐ๋ณต๋๋ ์ค์ ์ธ๋๋ฐ ์ฌ์ฉํ ์๋ ์๋ค.
# lineCount = None
# with open('fileName', 'rb') as f:
# lineCount = Counter(f)
# print(lineCount)
#------------------
# 4. deque
#------------------
#
# ์ถ๊ฐ๋ ์ญ์ ๊ฐ ์์ชฝ์์ ๊ฐ๋ฅํ double ended queue.
from collections import deque
d = deque()
d.append('1')
d.append('2')
d.append('3')
print(len(d))
print(d[0])
print(d[-1])
d = deque([i for i in range(5)])
print(len(d))
d.popleft()
d.pop()
print(d)
d = deque(maxlen=30)
# 30 ๊ฐ ์ด์์ ์์ดํ
์ ๋ฃ์ผ๋ฉด, ์ผ์ชฝ์ ๊ฐ์ฅ ์์ ์๋ ๊ฒ์ด ํ์ด๋์ฌ ๊ฒ์ด๋ค.
d = deque([1,2,3,4,5])
d.extendleft([0])
d.extend([6,7,8])
print(d)
#------------------
# 5. namedtuple
#------------------
#
# ํํ์ ๊ธฐ๋ณธ์ ์ผ๋ก ๊ฐ์ ์ํ์ค๋ฅผ ์ผํ๋ก ๊ตฌ๋ถํ์ฌ ์ ์ฅํ ์ ์๋ ๋ถ๋ณ(immutable) ๋ฆฌ์คํธ์ด๋ค.
# ๋ฆฌ์คํธ์ ๊ฑฐ์ ๋น์ทํ์ง๋ง ๋ช๊ฐ์ง ์ค์ํ ์ฐจ์ด์ ์ด ์๋ค.
# ์ฃผ์ํ ์ ์ ๋ฆฌ์คํธ์ ๋ฌ๋ฆฌ, ํํ์ ์๋ ํญ๋ชฉ์ ์ฌํ ๋น ํ ์ ์๋ค๋ ๊ฒ์ด๋ค.
# ํํ ์์ ๊ฐ์ ์ ๊ทผํ๊ธฐ ์ํด์๋ ์๋์ ๊ฐ์ด ์ ์ ์ธ๋ฑ์ค๋ฅผ ์ฌ์ฉํ๋ค.
man = ('ali', 30)
print(man[0])
# namedetuples ์ฌ์ฉํ๋ฉด ํํ ์์ ๊ฐ๋ค์ ์ ๊ทผํ๊ธฐ ์ํด ์ ์ ์ธ๋ฑ์ค ๊ฐ๋ค์ ์ฌ์ฉํ ํ์๊ฐ ์๋ค.
# ์ฌ์ ํ์ด๋ผ๊ณ ์๊ฐํ ์ ์์ง๋ง, ์ฌ์ ํ๊ณผ ๋ฌ๋ฆฌ ๋ถ๋ณํ์ด๋ค.
from collections import namedtuple
Animal = namedtuple('Animal', 'name age type')
perry = Animal(name='perry', age=31, type='cat')
print(perry)
print(perry.name)
print(perry.age)
print(perry.type)
# ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ์ฌ ์ด๋ฆ๋ง์ผ๋ก ํํ์ ๋ฉค๋ฒ์ ์ ๊ทผํ ์ ์์์ ์ ์ ์๋ค.
# namedtuple๋ ์ธ์ 2๊ฐ๊ฐ ํ์ํ๋ค.(ํํ ์ด๋ฆ, ํ๋ ์ด๋ฆ)
# ์ ์์ ์์๋ tuple name: Animal์ด๊ณ ํ๋_names๋ ('name', 'age', 'type')
# nametuple์ ์๊ฐ๋ฌธ์ํ(self-document) ์ํจ๋ค.
# ํํ์ ๋ฉค๋ฒ์ ์ ๊ทผํ๊ธฐ ์ํด ์ ์ ์ธ๋ฑ์ค๋ฅผ ์ฌ์ฉํ ํ์๊ฐ ์์ผ๋ฏ๋ก ์ฝ๋๋ฅผ ์ ์ง๊ด๋ฆฌํ๊ธฐ๊ฐ ๋ ์ฝ๋ค.
# namedtuple์ instance๋ instance ๋น ์ฌ์ ๋ค์ ๊ฐ์ง์ง ์์ผ๋ฏ๋ก, ์ผ๋ฐ ํํ๋ณด๋ค ๋ ๊ฐ๋ณ๊ณ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ ์ค์ผ ์ ์๋ค.
# ๊ทธ๋์ ์ฌ์ ํ๋ณด๋ค๋ ๋ ๋น ๋ฅด๋ค. ๊ทธ๋ฌ๋ ํํ์ด๊ธฐ ๋๋ฌธ์ ๋ถ๋ณ์์ ๋ช
์ฌํด์ผํ๋ค.
from collections import namedtuple
Animal = namedtuple('Animal', 'name age type')
perry = Animal(name='perry', age=31, type='cat')
print(perry)
#perry.age = 20 # AttriguteError: can't set attribute
from collections import namedtuple
Animal = namedtuple('Animal', 'name age type')
perry = Animal(name='perry', age=31, type='cat')
print(perry[0])
print(perry._asdict())
#-------------------------
# 6. enum.Enum
#-------------------------
from collections import namedtuple
from enum import Enum
class Species(Enum):
cat = 1
dog = 2
horse = 3
aardvark = 4
butterfly = 5
owl = 6
platypus = 7
dragon = 8
unicorn = 9
kitten = 1
puppy = 2
Animal = namedtuple('Animal', 'name age type')
perry = Animal(name='Perry', age=31, type=Species.cat)
dragon = Animal(name='Dragon', age=4, type=Species.dragon)
tom = Animal(name='Tom', age=75, type=Species.cat)
charlie = Animal(name='Charlie', age=2, type=Species.kitten)
print(perry.type == tom.type)
print(charlie.type == tom.type)