Skip to content

Commit 69337e3

Browse files
committed
intermediate python
1 parent 10c5d0c commit 69337e3

File tree

5 files changed

+173
-0
lines changed

5 files changed

+173
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from collections import defaultdict
2+
3+
# defaultdict you do not need to check whether a key is present or not
4+
# default dict가 편한점!!!
5+
colours = (
6+
('Yasoob', 'Yellow'),
7+
('Ali', 'Blue'),
8+
('Arham', 'Green'),
9+
('Ali', 'Black'),
10+
('Yasoob', 'Red'),
11+
('Ahmed', 'Silver'),
12+
)
13+
14+
favourite_colours = defaultdict(list)
15+
16+
for name, colour in colours:
17+
favourite_colours[name].append(colour)
18+
19+
print(favourite_colours)
20+
21+
# output
22+
# defaultdict(<type 'list'>,
23+
# {'Arham': ['Green'],
24+
# 'Yasoob': ['Yellow', 'Red'],
25+
# 'Ahmed': ['Silver'],
26+
# 'Ali': ['Blue', 'Black']
27+
# })
28+
29+
from collections import defaultdict
30+
tree = lambda: defaultdict(tree)
31+
some_dict = tree()
32+
some_dict['colours']['favourite'] = "yellow"
33+
# Works fine
34+
import json
35+
print(json.dumps(some_dict))
36+
# Output: {"colours": {"favourite": "yellow"}}
37+
38+
from collections import OrderedDict
39+
40+
colours = OrderedDict([("Red", 198), ("Green", 170), ("Blue", 160)])
41+
for key, value in colours.items():
42+
print(key, value)
43+
# Output:
44+
# Red 198
45+
# Green 170
46+
# Blue 160
47+
# Insertion order is preserved
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from collections import Counter
2+
3+
colours = (
4+
('Yasoob', 'Yellow'),
5+
('Ali', 'Blue'),
6+
('Arham', 'Green'),
7+
('Ali', 'Black'),
8+
('Yasoob', 'Red'),
9+
('Ahmed', 'Silver'),
10+
)
11+
12+
favs = Counter(name for name, colour in colours)
13+
print(favs)
14+
# Output: Counter({
15+
# 'Yasoob': 2,
16+
# 'Ali': 2,
17+
# 'Arham': 1,
18+
# 'Ahmed': 1
19+
# })
20+
21+
with open('filename', 'rb') as f:
22+
line_count = Counter(f)
23+
print(line_count)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from collections import deque
2+
3+
## 값 넣기
4+
d = deque()
5+
d.append('1')
6+
d.append('2')
7+
d.append('3')
8+
9+
print(len(d))
10+
# Output: 3
11+
12+
print(d[0])
13+
# Output: '1'
14+
15+
print(d[-1])
16+
# Output: '3'
17+
18+
d = deque([1,2,3,4,5])
19+
d.extendleft([0])
20+
d.extend([6,7,8])
21+
print(d)
22+
# Output: deque([0, 1, 2, 3, 4, 5, 6, 7, 8])
23+
24+
25+
## 값 빼기
26+
27+
d = deque(range(5))
28+
print(len(d))
29+
# Output: 5
30+
31+
d.popleft()
32+
# Output: 0
33+
34+
d.pop()
35+
# Output: 4
36+
37+
print(d)
38+
# Output: deque([1, 2, 3])
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from collections import namedtuple
2+
from enum import Enum
3+
4+
class Species(Enum):
5+
cat = 1
6+
dog = 2
7+
horse = 3
8+
aardvark = 4
9+
butterfly = 5
10+
owl = 6
11+
platypus = 7
12+
dragon = 8
13+
unicorn = 9
14+
# The list goes on and on...
15+
16+
# But we don't really care about age, so we can use an alias.
17+
kitten = 1
18+
puppy = 2
19+
20+
Animal = namedtuple('Animal', 'name age type')
21+
perry = Animal(name="Perry", age=31, type=Species.cat)
22+
drogon = Animal(name="Drogon", age=4, type=Species.dragon)
23+
tom = Animal(name="Tom", age=75, type=Species.cat)
24+
charlie = Animal(name="Charlie", age=2, type=Species.kitten)
25+
26+
# And now, some tests.
27+
charlie.type == tom.type
28+
True
29+
charlie.type
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from collections import namedtuple
2+
# you can not reassign an item in a tuple. In order to access the value in a tuple you use integer indexes like:
3+
4+
5+
Animal = namedtuple('Animal', 'name age type')
6+
perry = Animal(name="perry", age=31, type="cat")
7+
8+
print(perry)
9+
# Output: Animal(name='perry', age=31, type='cat')
10+
11+
print(perry.name)
12+
# Output: 'perry'
13+
14+
from collections import namedtuple
15+
16+
Animal = namedtuple('Animal', 'name age type')
17+
perry = Animal(name="perry", age=31, type="cat")
18+
perry.age = 42
19+
20+
# Output: Traceback (most recent call last):
21+
# File "", line 1, in
22+
# AttributeError: can't set attribute
23+
24+
from collections import namedtuple
25+
26+
Animal = namedtuple('Animal', 'name age type')
27+
perry = Animal(name="perry", age=31, type="cat")
28+
print(perry[0])
29+
# Output: perry
30+
31+
from collections import namedtuple
32+
33+
Animal = namedtuple('Animal', 'name age type')
34+
perry = Animal(name="Perry", age=31, type="cat")
35+
print(perry._asdict())
36+
# Output: OrderedDict([('name', 'Perry'), ('age', 31), ..

0 commit comments

Comments
 (0)