-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionary.py
More file actions
88 lines (64 loc) · 2.3 KB
/
dictionary.py
File metadata and controls
88 lines (64 loc) · 2.3 KB
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
# Dictionary = A set of key-value pairs. A collection which is unordered but indexed, and changeable.
# No duplicate members (set인데 key-value 쌍)
capitals = {"USA": "Washington D.C.",
"India": "New Delhi",
"China": "Beijing",
"Russia": "Moscow"}
# dictionary의 모든 method와 attribute를 보려면
# print(dir(capitals))
# method와 attribute의 설명을 보려면
# print(help(capitals))
print(capitals["USA"])
print(capitals.get("Japan")) # 잘 사용하지 않는 표현
print()
if "USA" in capitals:
print("That capital exists")
else:
print("That capital doesn't exist")
if capitals.get("Russia"):
print("That capital exists")
else:
print("That capital doesn't exist")
print()
# dictionary의 값을 추가, 변경 가능
capitals["Germany"] = "Berlin" # 추가
print(capitals)
capitals["USA"] = "USA Capital" # 변경
print(capitals)
# key: value 쌍을 없애기 위해선
del capitals["China"] # 삭제
print(capitals)
capitals.pop("India")
print(capitals)
# 가장 최근의 쌍을 없애기 위해선
capitals.popitem()
print(capitals)
# capitals.clear()
print()
# dictionary의 모든 key를 얻으려면 (key 객체를 반환)
keys = capitals.keys()
print(keys) # dict_keys(['USA', 'India', 'Russia'])
keys = list(keys)
print(keys) # ['USA', 'India', 'Russia']
for key in capitals.keys():
print(key)
print()
# dictionary의 모든 value를 얻으려면 (value 객체를 반환)
values = capitals.values() # dict_values(['usa capital', 'New Delhi', 'Moscow'])
print(values)
values = list(values) # ['usa capital', 'New Delhi', 'Moscow']
print(values)
for value in capitals.values():
print(value)
print()
# dictionary 객체를 반환 (Tuple의 2d list와 비슷한 모양)
item = capitals.items() # dict_items([('USA', 'usa capital'), ('India', 'New Delhi'), ('Russia', 'Moscow')])
print(item)
item = list(item) # [('USA', 'usa capital'), ('India', 'New Delhi'), ('Russia', 'Moscow')]
print(item)
for key, value in capitals.items():
print(f"{key}: {value}")
print()
# constructor(생성자)
thisdict = dict(brand = "Ford", model = "Mustang", year = 1964, for_sale = True)
print(thisdict)