Bạn cũng sẽ tìm hiểu một số phương pháp tích hợp phổ biến nhất được sử dụng trên từ điển
Đây là những gì chúng tôi sẽ đề cập
Định nghĩa từ điển
Xác định một từ điển trống
Xác định một từ điển với các mục
Tổng quan về khóa và giá trị 1. Tìm số cặp
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
9 có trong từ điển 2. Xem tất cả
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
9 cặp 3. Xem tất cả
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
1 4. Xem tất cả
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
2
Truy cập các mục riêng lẻ
Sửa đổi từ điển
Thêm mục mới
Cập nhật các mục
Xóa mục
Cách tạo từ điển bằng Python
Một từ điển trong Python được tạo thành từ các cặp khóa-giá trị
Trong hai phần tiếp theo, bạn sẽ thấy hai cách tạo từ điển
Cách thứ nhất là sử dụng một bộ dấu ngoặc nhọn,
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
3 và cách thứ hai là sử dụng hàm
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
4 tích hợp
Cách tạo một từ điển trống trong Python
Để tạo một từ điển trống, trước tiên hãy tạo một tên biến sẽ là tên của từ điển
Sau đó, gán biến cho một tập hợp các dấu ngoặc nhọn trống,
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
3
#create an empty dictionary
my_dictionary = {}
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
Một cách khác để tạo từ điển trống là sử dụng hàm
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
4 mà không truyền bất kỳ đối số nào
Nó hoạt động như một hàm tạo và tạo một từ điển trống
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
Cách tạo từ điển với các mục trong Python
Để tạo từ điển với các mục, bạn cần bao gồm các cặp khóa-giá trị bên trong dấu ngoặc nhọn
Cú pháp chung cho điều này là như sau
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
0
Hãy phá vỡ nó
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
7 là tên biến. Đây là tên mà từ điển sẽ có
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
8 là toán tử gán gán cặp
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
9 cho
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
7
Bạn khai báo một từ điển với một bộ dấu ngoặc nhọn,
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
3
Bên trong dấu ngoặc nhọn, bạn có một cặp khóa-giá trị. Các khóa được phân tách khỏi các giá trị được liên kết của chúng bằng dấu hai chấm,
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
62
Hãy xem một ví dụ về việc tạo từ điển với các mục
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
Trong ví dụ trên, có một dãy các phần tử trong dấu ngoặc nhọn
Cụ thể, có ba cặp khóa-giá trị.
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
63,
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
64 và
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
65
Các phím là
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
66,
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
67 và
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
68. Các giá trị liên quan của chúng lần lượt là
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
69,
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
50 và
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
51
Khi có nhiều cặp khóa-giá trị trong từ điển, mỗi cặp khóa-giá trị được phân tách với cặp tiếp theo bằng dấu phẩy,
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
52
Hãy xem một ví dụ khác
Giả sử lần này bạn muốn tạo một từ điển với các mục bằng cách sử dụng hàm
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
4
Bạn sẽ đạt được điều này bằng cách sử dụng
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
4 và chuyển các dấu ngoặc nhọn với chuỗi các cặp khóa-giá trị được đặt trong chúng làm đối số cho hàm
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
Điều đáng nói là phương pháp
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
55, đây là một cách khác để tạo từ điển
Nó lấy một chuỗi các mục được xác định trước làm đối số và trả về một từ điển mới với các mục trong chuỗi được đặt làm khóa được chỉ định của từ điển
Bạn có thể tùy chọn đặt giá trị cho tất cả các khóa, nhưng theo mặc định, giá trị cho các khóa sẽ là
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
56
Cú pháp chung của phương thức như sau
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
6
Hãy xem một ví dụ về cách tạo từ điển bằng cách sử dụng
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
55 mà không cần đặt giá trị cho tất cả các khóa
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
5
Bây giờ hãy xem một ví dụ khác đặt giá trị giống nhau cho tất cả các khóa trong từ điển
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
6
Tổng quan về Khóa và Giá trị trong Từ điển bằng Python
Các khóa bên trong từ điển Python chỉ có thể thuộc loại không thay đổi
Các kiểu dữ liệu bất biến trong Python là
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
58,
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
59,
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
60,
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
61 và
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
62
Khóa từ điển không được thuộc loại có thể thay đổi, chẳng hạn như
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
63,
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
64 hoặc
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
65
Vì vậy, giả sử bạn có từ điển sau
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
5
Các khóa trong từ điển là các loại dữ liệu
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
66,
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
67,
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
68 và
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
69, tất cả đều được chấp nhận
Nếu bạn cố tạo khóa thuộc loại có thể thay đổi, bạn sẽ gặp lỗi - cụ thể lỗi sẽ là
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
50
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
1
Trong ví dụ trên, tôi đã cố gắng tạo một khóa thuộc loại
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
51 (một loại dữ liệu có thể thay đổi). Điều này dẫn đến lỗi
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
52
Khi nói đến các giá trị bên trong từ điển Python, không có giới hạn nào. Các giá trị có thể thuộc bất kỳ loại dữ liệu nào - nghĩa là chúng có thể thuộc cả hai loại có thể thay đổi và không thể thay đổi
Một điều khác cần lưu ý về sự khác biệt giữa các khóa và giá trị trong từ điển Python, đó là các khóa là duy nhất. Điều này có nghĩa là một khóa chỉ có thể xuất hiện một lần trong từ điển, trong khi có thể có các giá trị trùng lặp
Cách tìm số cặp #create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
9 có trong từ điển bằng Python
Hàm
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
54 trả về tổng chiều dài của đối tượng được truyền dưới dạng đối số
Khi một từ điển được truyền dưới dạng đối số cho hàm, nó sẽ trả về tổng số cặp khóa-giá trị có trong từ điển
Đây là cách bạn tính số cặp khóa-giá trị bằng cách sử dụng ________ 454
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
0
Cách xem tất cả các cặp #create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
9 có trong từ điển bằng Python
Để xem mọi cặp khóa-giá trị có trong từ điển, hãy sử dụng phương thức
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
57 tích hợp
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
1
Phương thức
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
57 trả về một danh sách các bộ chứa các cặp khóa-giá trị nằm trong từ điển
Cách xem tất cả #create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
1 có trong từ điển bằng Python
Để xem tất cả các khóa bên trong từ điển, hãy sử dụng phương pháp tích hợp sẵn
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
10
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
2
Phương thức
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
10 trả về một danh sách chỉ chứa các khóa nằm trong từ điển
Cách xem tất cả #create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
2 có trong từ điển bằng Python
Để xem tất cả các giá trị trong từ điển, hãy sử dụng phương thức tích hợp sẵn
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
13
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
3
Phương thức
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
13 trả về một danh sách chỉ chứa các giá trị bên trong từ điển
Cách truy cập các mục riêng lẻ trong từ điển bằng Python
Khi làm việc với danh sách, bạn truy cập các mục danh sách bằng cách nhắc đến tên danh sách và sử dụng ký hiệu dấu ngoặc vuông. Trong dấu ngoặc vuông, bạn chỉ định số chỉ mục (hoặc vị trí) của mục
Bạn không thể làm chính xác như vậy với từ điển
Khi làm việc với từ điển, bạn không thể truy cập một phần tử bằng cách tham chiếu số chỉ mục của nó, vì từ điển chứa các cặp khóa-giá trị
Thay vào đó, bạn truy cập mục bằng cách sử dụng tên từ điển và ký hiệu dấu ngoặc vuông, nhưng lần này trong dấu ngoặc vuông bạn chỉ định một khóa
Mỗi khóa tương ứng với một giá trị cụ thể, vì vậy bạn đề cập đến khóa được liên kết với giá trị bạn muốn truy cập
Cú pháp chung để làm như vậy là như sau
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
4
Hãy xem ví dụ sau về cách truy cập một mục trong từ điển Python
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
5
Điều gì sẽ xảy ra khi bạn cố gắng truy cập một khóa không tồn tại trong từ điển?
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
6
Kết quả là
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
15 vì không có khóa nào như vậy trong từ điển
Một cách để tránh điều này xảy ra là trước tiên hãy tìm kiếm xem khóa có trong từ điển ngay từ đầu không
Bạn làm điều này bằng cách sử dụng từ khóa
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
16 trả về giá trị Boolean. Nó trả về
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
17 nếu khóa có trong từ điển và
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
18 nếu không có
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
7
Một cách khác để giải quyết vấn đề này là truy cập các mục trong từ điển bằng cách sử dụng phương pháp
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
19
Bạn chuyển khóa mà bạn đang tìm kiếm làm đối số và
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
19 trả về giá trị tương ứng với khóa đó
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
8
Như bạn nhận thấy, khi bạn đang tìm kiếm một khóa không tồn tại, theo mặc định,
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
19 sẽ trả về
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
56 thay vì
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
15
Nếu thay vì hiển thị giá trị
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
56 mặc định đó, bạn muốn hiển thị một thông báo khác khi khóa không tồn tại, bạn có thể tùy chỉnh
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
19 bằng cách cung cấp một giá trị khác
Bạn làm như vậy bằng cách chuyển giá trị mới làm đối số tùy chọn thứ hai cho phương thức
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
19
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
9
Bây giờ khi bạn đang tìm kiếm một khóa và nó không có trong từ điển, bạn sẽ thấy thông báo
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
07 xuất hiện trên bảng điều khiển
Cách sửa đổi từ điển trong Python
Từ điển có thể thay đổi, có nghĩa là chúng có thể thay đổi
Chúng có thể phát triển và thu nhỏ trong suốt vòng đời của chương trình
Có thể thêm các mục mới, các mục hiện có có thể được cập nhật với các giá trị mới và có thể xóa các mục
Cách thêm mục mới vào từ điển trong Python
Để thêm một cặp khóa-giá trị vào từ điển, hãy sử dụng ký hiệu dấu ngoặc vuông
Cú pháp chung để làm như vậy là như sau
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
00
Đầu tiên, chỉ định tên của từ điển. Sau đó, trong ngoặc vuông, tạo một khóa và gán cho nó một giá trị
Giả sử bạn đang bắt đầu với một từ điển trống
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
01
Đây là cách bạn sẽ thêm một cặp khóa-giá trị vào
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
08
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
02
Đây là cách bạn sẽ thêm một cặp khóa-giá trị mới khác
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
03
Hãy nhớ rằng nếu khóa bạn đang cố gắng thêm đã tồn tại trong từ điển đó và bạn đang gán cho nó một giá trị khác, thì khóa đó sẽ được cập nhật
Hãy nhớ rằng các khóa cần phải là duy nhất
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
04
Nếu bạn muốn ngăn việc vô tình thay đổi giá trị của một khóa đã tồn tại, bạn có thể muốn kiểm tra xem khóa bạn đang cố gắng thêm đã có trong từ điển chưa
Bạn làm điều này bằng cách sử dụng từ khóa
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
16 như chúng tôi đã thảo luận ở trên
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
05
Cách cập nhật các mục trong từ điển bằng Python
Cập nhật các mục trong từ điển hoạt động theo cách tương tự như thêm các mục vào từ điển
Khi bạn biết mình muốn cập nhật giá trị của một khóa hiện có, hãy sử dụng cú pháp chung sau mà bạn đã thấy trong phần trước
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
06
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
07
Để cập nhật từ điển, bạn cũng có thể sử dụng phương pháp tích hợp sẵn
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
10
Phương pháp này đặc biệt hữu ích khi bạn muốn cập nhật nhiều hơn một giá trị trong từ điển cùng một lúc
Giả sử bạn muốn cập nhật khóa
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
66 và
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
67 trong
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
08 và thêm khóa mới,
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
14
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
08
Phương thức
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
10 lấy một bộ các cặp khóa-giá trị
Các khóa đã tồn tại đã được cập nhật với các giá trị mới đã được chỉ định và một cặp khóa-giá trị mới đã được thêm vào
Phương thức
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
10 cũng hữu ích khi bạn muốn thêm nội dung của từ điển này vào từ điển khác
Giả sử bạn có một từ điển,
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
17 và từ điển thứ hai,
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
18
Nếu bạn muốn hợp nhất nội dung của
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
18 với nội dung của
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
17, hãy sử dụng phương thức
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
10
Tất cả các cặp khóa-giá trị có trong
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
18 sẽ được thêm vào cuối từ điển
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
17
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
09
Cách xóa các mục khỏi từ điển trong Python
Một trong những cách để xóa một khóa cụ thể và giá trị được liên kết của nó khỏi từ điển là sử dụng từ khóa
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
24
Cú pháp để làm như vậy là như sau
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
0
Ví dụ: đây là cách bạn xóa khóa
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
68 khỏi từ điển
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
26
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
1
Nếu bạn muốn xóa một khóa nhưng cũng muốn lưu giá trị đã xóa đó, hãy sử dụng phương thức
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
27 tích hợp
Phương thức
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
27 xóa nhưng cũng trả về khóa bạn chỉ định. Bằng cách này, bạn có thể lưu trữ giá trị đã xóa trong một biến để sử dụng hoặc truy xuất sau này
Bạn chuyển khóa bạn muốn xóa làm đối số cho phương thức
Đây là cú pháp chung để làm điều đó
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
2
Để xóa khóa
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
68 khỏi ví dụ trên, nhưng lần này sử dụng phương thức
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
27 và lưu giá trị được liên kết với khóa vào một biến, hãy làm như sau
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
3
Nếu bạn chỉ định một khóa không tồn tại trong từ điển, bạn sẽ nhận được thông báo lỗi
#create a dictionary with dict()
my_information = dict({'name': 'Dionysia' ,'age': 28,'location': 'Athens'})
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
15
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
4
Một cách giải quyết vấn đề này là truyền đối số thứ hai cho phương thức
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
27
Bằng cách bao gồm đối số thứ hai sẽ không có lỗi. Thay vào đó, sẽ có lỗi im lặng nếu khóa không tồn tại và từ điển sẽ không thay đổi
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
5
Phương thức
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
27 xóa một khóa cụ thể và giá trị liên quan của nó – nhưng nếu bạn chỉ muốn xóa cặp khóa-giá trị cuối cùng khỏi từ điển thì sao?
Đối với điều đó, thay vào đó hãy sử dụng phương pháp
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
34 tích hợp
Đây là cú pháp chung cho phương thức
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
34
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
6
Phương thức
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
34 không nhận đối số, nhưng loại bỏ và trả về cặp khóa-giá trị cuối cùng từ từ điển
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
7
Cuối cùng, nếu bạn muốn xóa tất cả các cặp khóa-giá trị khỏi từ điển, hãy sử dụng phương thức tích hợp sẵn
#create an empty dictionary
my_dictionary = dict()
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>
37
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
print(my_information)
#check data type
print(type(my_information))
#output
#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#<class 'dict'>
8
Sử dụng phương pháp này sẽ để lại cho bạn một từ điển trống
Sự kết luận
Và bạn có nó rồi đấy. Bây giờ bạn đã biết những kiến thức cơ bản về từ điển trong Python
Tôi hy vọng bạn tìm thấy bài viết này hữu ích
Để tìm hiểu thêm về ngôn ngữ lập trình Python, hãy xem Chứng chỉ máy tính khoa học với Python của freeCodeCamp
Bạn sẽ bắt đầu từ những điều cơ bản và học theo cách tương tác và thân thiện với người mới bắt đầu. Cuối cùng, bạn cũng sẽ xây dựng 5 dự án để áp dụng vào thực tế và giúp củng cố những gì bạn đã học được
Cảm ơn đã đọc và mã hóa hạnh phúc
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
Dionysia Lemonaki
Học một cái gì đó mới mỗi ngày và viết về nó
Nếu bài viết này hữu ích, hãy tweet nó
Học cách viết mã miễn phí. Chương trình giảng dạy mã nguồn mở của freeCodeCamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu
Bạn có thể lồng từ điển vào danh sách Python không?
Danh sách Python có thể chứa từ điển dưới dạng mục danh sách .
Danh sách có thể chứa từ điển không?
Từ điển có thể được chứa trong danh sách và ngược lại.