1. Giới thiệu
Dictionary là cấu trúc dữ liệu mà các phần tử của chúng được lưu dưới dạng key, value
Khai báo dictionary
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
d = {"key1": "value1", "key2": "value2", "key3": "value3", ..., "keyn": "valuen"} | |
# Hoặc | |
d = dict({"key1": "value1", "key2": "value2", "key3": "value3"}) |
- key là duy nhất
- Các phần tử đều phải có key
- key có phân biệt hoa thường
- key là chuỗi hoặc số
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
person = { | |
'name': 'Trần Văn Lộc', | |
'address': 'Đà Nẵng', | |
'age': 23 | |
} | |
print(person) # {'name': 'Trần Văn Lộc', 'address': 'Đà Nẵng', 'age': 23} |
Vì các phần tử dictionary được sắp xếp không theo thứ tự nên không thể truy cập bằng index như string, tuple hay list được. Thay vào đó ta truy cập đến các phần tử của dictionary dựa vào key
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
person = { | |
'name': 'Trần Văn Lộc', | |
'address': 'Đà Nẵng', | |
'age': 23 | |
} | |
print(person['name']) # Trần Văn Lộc | |
print(person['address']) # Đà Nẵng | |
print(person['age']) # 23 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
person = { | |
'name': 'Trần Văn Lộc', | |
'address': 'Đà Nẵng', | |
'age': 23 | |
} | |
print("Gía trị cũ:", person) # Gía trị cũ: {'name': 'Trần Văn Lộc', 'address': 'Đà Nẵng', 'age': 23} | |
person['address'] = 'Việt Nam' | |
person['age'] = 25 | |
print("Gía trị mới:", person) # Gía trị mới: {'name': 'Trần Văn Lộc', 'address': 'Việt Nam', 'age': 25} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
person = { | |
'name': 'Trần Văn Lộc', | |
'address': 'Đà Nẵng', | |
'age': 23 | |
} | |
print("Truoc khi xoa:", person) | |
del person['name'] | |
print("Sau khi xoa:", person) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
person = { | |
'name': 'Trần Văn Lộc', | |
'address': 'Đà Nẵng', | |
'age': 23 | |
} | |
del person | |
print(person) # name 'person' is not defined |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
person = { | |
'name': 'Trần Văn Lộc', | |
'address': 'Đà Nẵng', | |
'age': 23 | |
} | |
print(person.clear()) # None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
person = { | |
'name': 'Trần Văn Lộc', | |
'address': 'Đà Nẵng', | |
'age': 23, | |
} | |
# check key đã tồn tại chưa | |
if 'male' not in person.keys(): | |
person['male'] = True | |
else: | |
print("Key đã tồn tại!!!") | |
print(person) # {'name': 'Trần Văn Lộc', 'address': 'Đà Nẵng', 'age': 23, 'male': True} |
0 nhận xét:
Đăng nhận xét