Python cho phép các giá trị trong từ điển là bất kỳ loại nào – chuỗi, số nguyên, danh sách, từ điển khác, boolean, v.v. Tuy nhiên, các khóa phải luôn là một loại dữ liệu bất biến, chẳng hạn như chuỗi, số hoặc bộ dữ liệu
Trong khối mã ví dụ, bạn có thể thấy các phím là chuỗi hoặc số [int hoặc float]. Mặt khác, các giá trị là nhiều loại dữ liệu khác nhau
Bạn sẽ học cách tạo từ điển, truy cập các phần tử bên trong chúng và cách sửa đổi chúng tùy theo nhu cầu của bạn
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'}
#
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'}
#
4 tích hợpCá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'}
#
3#create an empty dictionary
my_dictionary = {}
print[my_dictionary]
#to check the data type use the type[] function
print[type[my_dictionary]]
#output
#{}
#
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'}
#
4 mà không truyền bất kỳ đối số nàoNó 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
#{}
#
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ó
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'} #
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'} #
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'} #
7#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'} #
- Bạn khai báo một từ điển với một bộ dấu ngoặc nhọn,
3#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'} #
- 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,
2dictionary_name = dict.fromkeys[sequence,value]
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'}
#
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]
5Cá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}
1Khi 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}
2Hã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'}
#
4Bạ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'}
#
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'}
#
Đ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ểnNó 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}
6Cú 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'}
2Khó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'}
5Vì vậy, giả sử bạn có từ điển sau
my_dictionary = {True: "True", 1: 1, 1.1: 1.1, "one": 1, "languages": ["Python"]}
print[my_dictionary]
#output
#{True: 1, 1.1: 1.1, 'one': 1, 'languages': ['Python']}
Các khóa trong từ điển là các loại dữ liệu
#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'}
6, #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'}
7, #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'}
8 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'}
9, tất cả đều được chấp nhậnNếu bạn cố gắng tạo một khóa thuộc loại có thể thay đổi, bạn sẽ gặp lỗi - cụ thể lỗi sẽ là
my_dictionary = {True: "True", 1: 1, 1.1: 1.1, "one": 1, "languages": ["Python"]}
print[my_dictionary]
#output
#{True: 1, 1.1: 1.1, 'one': 1, 'languages': ['Python']}
0my_dictionary = {["Python"]: "languages"}
print[my_dictionary]
#output
#line 1, in
# my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
Trong ví dụ trên, tôi đã cố gắng tạo một khóa thuộc loại
my_dictionary = {True: "True", 1: 1, 1.1: 1.1, "one": 1, "languages": ["Python"]}
print[my_dictionary]
#output
#{True: 1, 1.1: 1.1, 'one': 1, 'languages': ['Python']}
1 [một loại dữ liệu có thể thay đổi]. Điều này dẫn đến lỗi my_dictionary = {True: "True", 1: 1, 1.1: 1.1, "one": 1, "languages": ["Python"]}
print[my_dictionary]
#output
#{True: 1, 1.1: 1.1, 'one': 1, 'languages': ['Python']}
2Khi 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'}
#
9 có trong từ điển bằng Python
#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'}
#
Hàm
my_dictionary = {True: "True", 1: 1, 1.1: 1.1, "one": 1, "languages": ["Python"]}
print[my_dictionary]
#output
#{True: 1, 1.1: 1.1, 'one': 1, 'languages': ['Python']}
4 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
my_dictionary = {True: "True", 1: 1, 1.1: 1.1, "one": 1, "languages": ["Python"]}
print[my_dictionary]
#output
#{True: 1, 1.1: 1.1, 'one': 1, 'languages': ['Python']}
4#create an empty dictionary
my_dictionary = dict[]
print[my_dictionary]
#to check the data type use the type[] function
print[type[my_dictionary]]
#output
#{}
#
0Cá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'}
#
9 có trong từ điển bằng Python
#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'}
#
Để xem mọi cặp khóa-giá trị có trong từ điển, hãy sử dụng phương thức
my_dictionary = {True: "True", 1: 1, 1.1: 1.1, "one": 1, "languages": ["Python"]}
print[my_dictionary]
#output
#{True: 1, 1.1: 1.1, 'one': 1, 'languages': ['Python']}
7 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
#{}
#
1Phương thức
my_dictionary = {True: "True", 1: 1, 1.1: 1.1, "one": 1, "languages": ["Python"]}
print[my_dictionary]
#output
#{True: 1, 1.1: 1.1, 'one': 1, 'languages': ['Python']}
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ểnCá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'}
#
1 có trong từ điển bằng Python
#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'}
#
Để 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
my_dictionary = {["Python"]: "languages"}
print[my_dictionary]
#output
#line 1, in
# my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
0#create an empty dictionary
my_dictionary = dict[]
print[my_dictionary]
#to check the data type use the type[] function
print[type[my_dictionary]]
#output
#{}
#
2Phương thức
my_dictionary = {["Python"]: "languages"}
print[my_dictionary]
#output
#line 1, in
# my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
0 trả về một danh sách chỉ chứa các khóa nằm trong từ điểnCá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'}
#
2 có trong từ điển bằng Python
#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'}
#
Để xem tất cả các giá trị bên trong từ điển, hãy sử dụng phương thức
my_dictionary = {["Python"]: "languages"}
print[my_dictionary]
#output
#line 1, in
# my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
3 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
#{}
#
3Phương thức
my_dictionary = {["Python"]: "languages"}
print[my_dictionary]
#output
#line 1, in
# my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
3 trả về một danh sách chỉ chứa các giá trị nằm trong từ điểnCá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
#{}
#
4Hã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
#{}
#
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
#{}
#
6Kết quả là
my_dictionary = {["Python"]: "languages"}
print[my_dictionary]
#output
#line 1, in
# my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
5 vì không có khóa nào như vậy trong từ điểnMộ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
my_dictionary = {["Python"]: "languages"}
print[my_dictionary]
#output
#line 1, in
# my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
6 trả về giá trị Boolean. Nó trả về my_dictionary = {["Python"]: "languages"}
print[my_dictionary]
#output
#line 1, in
# my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
7 nếu khóa có trong từ điển và my_dictionary = {["Python"]: "languages"}
print[my_dictionary]
#output
#line 1, in
# my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
8 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
#{}
#
7Mộ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
my_dictionary = {["Python"]: "languages"}
print[my_dictionary]
#output
#line 1, in
# my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
9Bạn chuyển khóa mà bạn đang tìm kiếm làm đối số và
my_dictionary = {["Python"]: "languages"}
print[my_dictionary]
#output
#line 1, in
# my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
9 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
#{}
#
8Như 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,
my_dictionary = {["Python"]: "languages"}
print[my_dictionary]
#output
#line 1, in
# my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
9 sẽ trả 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}
6 thay vì một my_dictionary = {["Python"]: "languages"}
print[my_dictionary]
#output
#line 1, in
# my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
5Nếu thay vì hiển thị giá trị
#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 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 my_dictionary = {["Python"]: "languages"}
print[my_dictionary]
#output
#line 1, in
# my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
9 bằng cách cung cấp một giá trị khácBạ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
my_dictionary = {["Python"]: "languages"}
print[my_dictionary]
#output
#line 1, in
# my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
9#create an empty dictionary
my_dictionary = dict[]
print[my_dictionary]
#to check the data type use the type[] function
print[type[my_dictionary]]
#output
#{}
#
9Bâ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
#{}
#
07 xuất hiện trên bảng điều khiểnCá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
#{}
#
08dictionary_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}
3Hã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}
4Nế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
my_dictionary = {["Python"]: "languages"}
print[my_dictionary]
#output
#line 1, in
# my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
6 như chúng tôi đã thảo luận ở trêndictionary_name = {key: value}
5Cá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 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}
6dictionary_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
#{}
#
10Phươ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
#{}
#
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
#{}
#
14dictionary_name = {key: value}
8Phươ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
#{}
#
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
#{}
#
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ácGiả 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
#{}
#
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
#{}
#
18Nế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
#{}
#
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
#{}
#
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
#{}
#
10Tấ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
#{}
#
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
#{}
#
17dictionary_name = {key: value}
9Cá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
#{}
#
24Cú 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'}
#
0Ví 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
#{}
#
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'}
#
1Nế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
#{}
#
27 tích hợpPhươ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
#{}
#
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àyBạ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'}
#
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
#{}
#
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'}
#
3Nế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
my_dictionary = {["Python"]: "languages"}
print[my_dictionary]
#output
#line 1, in
# my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
5#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'}
#
4Mộ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
#{}
#
27Bằ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'}
#
5Phươ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
#{}
#
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
#{}
#
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
#{}
#
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'}
#
6Phươ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
#{}
#
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'}
#
7Cuố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
#{}
#
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'}
#
8Sử 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
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