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
1 2 3 4
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 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 rỗ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
dictionary_name = {key: value}
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,
dictionary_name = dict.fromkeys(sequence,value)
2
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ị.
dictionary_name = dict.fromkeys(sequence,value)
3,
dictionary_name = dict.fromkeys(sequence,value)
4 và
dictionary_name = dict.fromkeys(sequence,value)
5
Các chìa khóa là
dictionary_name = dict.fromkeys(sequence,value)
6,
dictionary_name = dict.fromkeys(sequence,value)
7 và
dictionary_name = dict.fromkeys(sequence,value)
8. Các giá trị liên kết của chúng lần lượt là
dictionary_name = dict.fromkeys(sequence,value)
9,
#create sequence of strings
cities = ('Paris','Athens', 'Madrid')
#create the dictionary, `my_dictionary`, using the fromkeys() method
my_dictionary = dict.fromkeys(cities)
print(my_dictionary)
#{'Paris': None, 'Athens': None, 'Madrid': None}
0 và
#create sequence of strings
cities = ('Paris','Athens', 'Madrid')
#create the dictionary, `my_dictionary`, using the fromkeys() method
my_dictionary = dict.fromkeys(cities)
print(my_dictionary)
#{'Paris': None, 'Athens': None, 'Madrid': None}
1
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 sequence of strings
cities = ('Paris','Athens', 'Madrid')
#create the dictionary, `my_dictionary`, using the fromkeys() method
my_dictionary = dict.fromkeys(cities)
print(my_dictionary)
#{'Paris': None, 'Athens': None, 'Madrid': None}
2
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 sequence of strings
cities = ('Paris','Athens', 'Madrid')
#create the dictionary, `my_dictionary`, using the fromkeys() method
my_dictionary = dict.fromkeys(cities)
print(my_dictionary)
#{'Paris': None, 'Athens': None, 'Madrid': None}
5, đâ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 sequence of strings
cities = ('Paris','Athens', 'Madrid')
#create the dictionary, `my_dictionary`, using the fromkeys() method
my_dictionary = dict.fromkeys(cities)
print(my_dictionary)
#{'Paris': None, 'Athens': None, 'Madrid': None}
6
Cú pháp chung của phương thức như sau
dictionary_name = dict.fromkeys(sequence,value)
Hãy xem một ví dụ về cách tạo từ điển bằng cách sử dụng
#create sequence of strings
cities = ('Paris','Athens', 'Madrid')
#create the dictionary, `my_dictionary`, using the fromkeys() method
my_dictionary = dict.fromkeys(cities)
print(my_dictionary)
#{'Paris': None, 'Athens': None, 'Madrid': None}
5 mà không cần đặt giá trị cho tất cả các khóa
#create sequence of strings
cities = ('Paris','Athens', 'Madrid')
#create the dictionary, `my_dictionary`, using the fromkeys() method
my_dictionary = dict.fromkeys(cities)
print(my_dictionary)
#{'Paris': None, 'Athens': None, 'Madrid': None}
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 sequence of strings
cities = ('Paris','Athens', 'Madrid')
#create a single value
continent = 'Europe'
my_dictionary = dict.fromkeys(cities,continent)
print(my_dictionary)
#output
#{'Paris': 'Europe', 'Athens': 'Europe', 'Madrid': 'Europe'}
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 sequence of strings
cities = ('Paris','Athens', 'Madrid')
#create the dictionary, `my_dictionary`, using the fromkeys() method
my_dictionary = dict.fromkeys(cities)
print(my_dictionary)
#{'Paris': None, 'Athens': None, 'Madrid': None}
8,
#create sequence of strings
cities = ('Paris','Athens', 'Madrid')
#create the dictionary, `my_dictionary`, using the fromkeys() method
my_dictionary = dict.fromkeys(cities)
print(my_dictionary)
#{'Paris': None, 'Athens': None, 'Madrid': None}
9,
#create a sequence of strings
cities = ('Paris','Athens', 'Madrid')
#create a single value
continent = 'Europe'
my_dictionary = dict.fromkeys(cities,continent)
print(my_dictionary)
#output
#{'Paris': 'Europe', 'Athens': 'Europe', 'Madrid': 'Europe'}
0,
#create a sequence of strings
cities = ('Paris','Athens', 'Madrid')
#create a single value
continent = 'Europe'
my_dictionary = dict.fromkeys(cities,continent)
print(my_dictionary)
#output
#{'Paris': 'Europe', 'Athens': 'Europe', 'Madrid': 'Europe'}
1 và
#create a sequence of strings
cities = ('Paris','Athens', 'Madrid')
#create a single value
continent = 'Europe'
my_dictionary = dict.fromkeys(cities,continent)
print(my_dictionary)
#output
#{'Paris': 'Europe', 'Athens': 'Europe', 'Madrid': 'Europe'}
2
Khóa từ điển không được thuộc loại có thể thay đổi, chẳng hạn như
#create a sequence of strings
cities = ('Paris','Athens', 'Madrid')
#create a single value
continent = 'Europe'
my_dictionary = dict.fromkeys(cities,continent)
print(my_dictionary)
#output
#{'Paris': 'Europe', 'Athens': 'Europe', 'Madrid': 'Europe'}
3,
#create a sequence of strings
cities = ('Paris','Athens', 'Madrid')
#create a single value
continent = 'Europe'
my_dictionary = dict.fromkeys(cities,continent)
print(my_dictionary)
#output
#{'Paris': 'Europe', 'Athens': 'Europe', 'Madrid': 'Europe'}
4 hoặc
#create a sequence of strings
cities = ('Paris','Athens', 'Madrid')
#create a single value
continent = 'Europe'
my_dictionary = dict.fromkeys(cities,continent)
print(my_dictionary)
#output
#{'Paris': 'Europe', 'Athens': 'Europe', 'Madrid': 'Europe'}
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
#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 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 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 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'>
0 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ị bên trong từ điển, 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'>
3 trả về một danh sách chỉ chứa các giá trị nằm 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'>
#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 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 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
dictionary_name = {key: value}
0
Đầ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
dictionary_name = {key: value}
1
Đâ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
dictionary_name = {key: value}
2
Đây là cách bạn sẽ thêm một cặp khóa-giá trị mới khác
dictionary_name = {key: value}
3
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
dictionary_name = {key: value}
4
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
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 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
dictionary_name = {key: value}
6
dictionary_name = {key: value}
7
Để 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
dictionary_name = dict.fromkeys(sequence,value)
6 và
dictionary_name = dict.fromkeys(sequence,value)
7 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
dictionary_name = {key: value}
8
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 nhận 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
dictionary_name = {key: value}
9
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ị liên quan 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
dictionary_name = dict.fromkeys(sequence,value)
8 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 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
dictionary_name = dict.fromkeys(sequence,value)
8 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
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 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'>
34
Đâ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
#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 tích hợ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'>
8
Sử dụng phương pháp này sẽ để lại cho bạn một từ điển trống
Phần 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 năm dự án để đưa vào thực tế và giúp củng cố những gì bạn đã họ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
Từ điển có động trong Python không?
Chúng động và sẽ cập nhật khi từ điển cập nhật. Chúng tôi không thể sử dụng lập chỉ mục để truy xuất một mục cụ thể của đối tượng xem.
dict ={} trong Python là gì?
Cấu trúc bảng băm khóa/giá trị hiệu quả của Python được gọi là "dict". Nội dung của một dict có thể được viết dưới dạng một chuỗi key. cặp giá trị trong dấu ngoặc nhọn { }, e. g. chính tả = {key1. giá trị1, khóa2. giá trị2,. }. "Chính tả trống" chỉ là một cặp dấu ngoặc nhọn trống {}.
Làm cách nào để tạo từ điển trong thời gian chạy Python?
Tạo từ điển
. Chỉ cần kết hợp tất cả “key1. giá trị1, khóa2. value2,…” ghép cặp và kèm theo dấu ngoặc nhọn . Sự kết hợp của một khóa và giá trị của nó, tôi. e. "chìa khóa. value” đại diện cho một phần tử duy nhất của từ điển trong Python.
Làm cách nào để nối thêm từ điển trong Python?
Phương pháp 1. Sử dụng dấu += trên khóa có giá trị trống
.