Từ điển là một cấu trúc dữ liệu thiết yếu trong Python và bất kỳ ngôn ngữ lập trình hiện đại nào khác, chẳng hạn như Javascript và Swift. Chúng còn được gọi là bản đồ, hashmap, bảng băm và mảng kết hợp. Mặc dù tên gọi khác nhau, nhưng chúng có chung một đặc điểm — các phần tử của chúng là các cặp khóa-giá trị. Nói một cách đơn giản, một từ điển bao gồm một hoặc nhiều mục và chúng ta có thể sử dụng các khóa duy nhất để xác định từng mục này. Sử dụng một khóa cụ thể, chúng tôi sẽ có thể tìm thấy giá trị được liên kết với khóa đó
Nếu bạn đã từng học Python, có lẽ bạn đã biết rằng
>>> dict_missing = {'zero': 0, 'one': 1}
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
4 là một trong những kiểu dữ liệu dựng sẵn được sử dụng phổ biến nhất. Nó là viết tắt của từ điển, nhưng bạn có biết rằng có các loại dữ liệu từ điển khác trong Python không? . Chúng tôi cũng sẽ bao gồm các cách sử dụng cơ bản của riêng họChính tả tích hợp
Kiểu dữ liệu
>>> dict_missing = {'zero': 0, 'one': 1}
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
4 là cấu trúc dữ liệu ánh xạ tích hợp duy nhất như một phần của thư viện chuẩn Python. Cách sử dụng chính của nó rất đơn giản, như được tóm tắt dưới đâyMột số điểm cần thiết được liệt kê bên dưới liên quan đến việc sử dụng phổ biến kiểu dữ liệu
>>> dict_missing = {'zero': 0, 'one': 1}
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
4- Các phím cần phải được. Một đối tượng có thể băm nếu loại của nó thực hiện phương thức
>>> dict_missing = {'zero': 0, 'one': 1}
0 sao cho giá trị băm có thể được tính cho từng đối tượng của loại. Điều quan trọng là các đối tượng có thể băm không bao giờ được thay đổi trong suốt vòng đời của chúng, điều đó có nghĩa là các đối tượng có thể thay đổi như
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1>>> dict_missing = {'zero': 0, 'one': 1}
1 và
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1>>> dict_missing = {'zero': 0, 'one': 1}
4 không thể băm được và do đó không thể được sử dụng làm khóa, như minh họa bên dưới
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
>>> dict_example = {1: 1, 'three': 3, [4, 5]: 0}
>>> dict_example[[1,2]] = 5
Traceback [most recent call last]:
File "", line 1, in
TypeError: unhashable type: 'list'
- Nếu chúng ta truy cập một giá trị bằng phương pháp dấu ngoặc vuông [i. e. ,
>>> dict_missing = {'zero': 0, 'one': 1}
3], thông dịch viên sẽ nêu ra một
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1>>> dict_missing = {'zero': 0, 'one': 1}
4 nếu chìa khóa không có trong
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1>>> dict_missing = {'zero': 0, 'one': 1}
4. Do đó, một cách an toàn hơn để truy cập giá trị của một khóa cụ thể là sử dụng phương thức
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1>>> dict_missing = {'zero': 0, 'one': 1}
6. Khi khóa không tồn tại,
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1>>> dict_missing = {'zero': 0, 'one': 1}
7 sẽ được trả về trừ khi giá trị mặc định được đặt và trả về tương ứng
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
>>> dict_missing = {'zero': 0, 'one': 1}
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
- Chúng ta có thể sử dụng các phương thức
>>> dict_missing = {'zero': 0, 'one': 1}
8,
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1>>> dict_missing = {'zero': 0, 'one': 1}
9 và
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1>>> dict_missing = {'zero': 0, 'one': 1}
00 để truy cập lần lượt các khóa, giá trị và cặp khóa-giá trị của
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1>>> dict_missing = {'zero': 0, 'one': 1}
4 [được hiển thị dưới dạng bộ dữ liệu]. Chúng được gọi là , có nghĩa là chúng chỉ là các chế độ xem động của
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1>>> dict_missing = {'zero': 0, 'one': 1}
4 và chúng thay đổi khi
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1>>> dict_missing = {'zero': 0, 'one': 1}
4 thay đổi. Bên cạnh đó, các chế độ xem này có thể lặp lại để tất cả chúng đều có thể được sử dụng trong các vòng lặp for
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
>>> dict_missing = {'zero': 0, 'one': 1}
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
0lệnh mặc định
Là một lớp con của lớp
>>> dict_missing = {'zero': 0, 'one': 1}
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
4 dựng sẵn, một đối tượng >>> dict_missing = {'zero': 0, 'one': 1}
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
05 đôi khi được sử dụng như một đối tượng giống từ điển. Cần lưu ý rằng kiểu dữ liệu >>> dict_missing = {'zero': 0, 'one': 1}
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
05 không phải là một phần của thư viện chuẩn và nó có sẵn trong mô-đun >>> dict_missing = {'zero': 0, 'one': 1}
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
07Loại
>>> dict_missing = {'zero': 0, 'one': 1}
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
05 có các chức năng tương tự như lớp >>> dict_missing = {'zero': 0, 'one': 1}
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
4, ngoại trừ việc có thuộc tính >>> dict_missing = {'zero': 0, 'one': 1}
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
60. Hàm tạo của >>> dict_missing = {'zero': 0, 'one': 1}
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
05 là >>> dict_missing = {'zero': 0, 'one': 1}
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
62. Cụ thể, giá trị mặc định của đối số ________ 160 là ________ 07. Khi được đặt, nó được gọi mà không có đối số để tạo giá trị mặc định khi một khóa đã cho không có trong >>> dict_missing = {'zero': 0, 'one': 1}
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
05. Một ví dụ được đưa ra dưới đây>>> dict_missing = {'zero': 0, 'one': 1}
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
6Như đã trình bày ở trên, khi
>>> dict_missing = {'zero': 0, 'one': 1}
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
66 không có bất kỳ khóa nào, việc truy cập vào giá trị của khóa bị thiếu [e. g. , 'a'], >>> dict_missing = {'zero': 0, 'one': 1}
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
60 [i. e. , >>> dict_missing = {'zero': 0, 'one': 1}
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
68] được gọi sao cho 0 được trả về, bởi vì >>> dict_missing = {'zero': 0, 'one': 1}
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
69 trả về 0. Như vậy, tính năng nổi bật nhất của >>> dict_missing = {'zero': 0, 'one': 1}
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
05 là trả về các giá trị mặc định bằng cách gọi hàm >>> dict_example = {1: 1, 'three': 3, [4, 5]: 0}
>>> dict_example[[1,2]] = 5
Traceback [most recent call last]:
File "", line 1, in
TypeError: unhashable type: 'list'
81 cho các khóa bị thiếuMột điều cần lưu ý rằng đối số
>>> dict_missing = {'zero': 0, 'one': 1}
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
60 chỉ có thể được đặt với thứ gì đó có thể gọi được, chẳng hạn như hàm hoặc lambda. Vì vậy, ví dụ trên có thể được viết lại như dưới đây. Chúng tôi sử dụng hàm lambda >>> dict_example = {1: 1, 'three': 3, [4, 5]: 0}
>>> dict_example[[1,2]] = 5
Traceback [most recent call last]:
File "", line 1, in
TypeError: unhashable type: 'list'
83 để thay thế hàm >>> dict_missing = {'zero': 0, 'one': 1}
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
68>>> dict_example = {1: 1, 'three': 3, [4, 5]: 0}
>>> dict_example[[1,2]] = 5
Traceback [most recent call last]:
File "", line 1, in
TypeError: unhashable type: 'list'
8The OrderedDict
Một loại giống như từ điển hữu ích khác là
>>> dict_example = {1: 1, 'three': 3, [4, 5]: 0}
>>> dict_example[[1,2]] = 5
Traceback [most recent call last]:
File "", line 1, in
TypeError: unhashable type: 'list'
85, loại này cũng có sẵn trong mô-đun >>> dict_missing = {'zero': 0, 'one': 1}
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
07 dưới dạng loại >>> dict_missing = {'zero': 0, 'one': 1}
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
05. Sự khác biệt chính giữa loại >>> dict_example = {1: 1, 'three': 3, [4, 5]: 0}
>>> dict_example[[1,2]] = 5
Traceback [most recent call last]:
File "", line 1, in
TypeError: unhashable type: 'list'
85 và >>> dict_missing = {'zero': 0, 'one': 1}
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
4 trước đây là loại trước ghi nhớ thứ tự của các mục. Tuy nhiên, kể từ Python 3. 7, loại >>> dict_missing = {'zero': 0, 'one': 1}
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
4 cũng đã triển khai thứ tự của các cặp khóa-giá trị dựa trên trình tự chèn của chúng, do đó, sự khác biệt này về mặt theo dõi thứ tự đã biến mất. Bên cạnh đó, có hai điều đáng chú ý- Họ triển khai phương pháp
>>> dict_missing = {'zero': 0, 'one': 1}
71 khác nhau. Trong
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1>>> dict_missing = {'zero': 0, 'one': 1}
4 [Trăn 3. 7+], phương thức này loại bỏ và trả về một cặp khóa-giá trị bằng LIFO [i. e. , vào sau ra trước] thứ tự. Tuy nhiên, trong
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1>>> dict_example = {1: 1, 'three': 3, [4, 5]: 0}
85, có một đối số boolean cuối cùng mà chúng ta có thể đặt. Khi nó đúng, cặp khóa-giá trị bị xóa và trả về sẽ là thứ tự LIFO, khi nó sai, thứ tự sẽ là FIFO [i. e. , vào trước ra trước], như vậy
>>> dict_example[[1,2]] = 5
Traceback [most recent call last]:
File "", line 1, in
TypeError: unhashable type: 'list'>>> dict_example = {1: 1, 'three': 3, [4, 5]: 0}
85 cho phép chúng tôi linh hoạt hơn về thứ tự mà chúng tôi muốn sử dụng để xóa các mục. Các ví dụ thích hợp được đưa ra dưới đây
>>> dict_example[[1,2]] = 5
Traceback [most recent call last]:
File "", line 1, in
TypeError: unhashable type: 'list'
>>> dict_missing = {'zero': 0, 'one': 1}
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
7- Họ thực hiện kiểm tra bình đẳng khác nhau. Khi chúng tôi so sánh hai
>>> dict_example = {1: 1, 'three': 3, [4, 5]: 0}
85, thứ tự của các mục quan trọng. Tuy nhiên, khi chúng ta so sánh với
>>> dict_example[[1,2]] = 5
Traceback [most recent call last]:
File "", line 1, in
TypeError: unhashable type: 'list'>>> dict_missing = {'zero': 0, 'one': 1}
4s, thứ tự của các mục không thành vấn đề. Ngoài các phép so sánh trong các loại dữ liệu tương ứng của chúng, thứ tự không thành vấn đề khi chúng ta so sánh một
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1>>> dict_example = {1: 1, 'three': 3, [4, 5]: 0}
85 với một
>>> dict_example[[1,2]] = 5
Traceback [most recent call last]:
File "", line 1, in
TypeError: unhashable type: 'list'>>> dict_missing = {'zero': 0, 'one': 1}
4. Dưới đây là một số ví dụ
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
>>> dict_missing = {'zero': 0, 'one': 1}
>>> dict_missing['two']
Traceback [most recent call last]:
File "", line 1, in
KeyError: 'two'
>>> print[dict_missing.get['two']]
None
>>> print[dict_missing.get['two', -1]]
-1
3mang đi
Trong bài viết này, chúng ta đã tìm hiểu 3 từ điển được sử dụng phổ biến nhất trong Python. Khi nào chúng ta biết sử dụng cái nào?