Trong bài viết này, chúng ta sẽ tìm hiểu về cấu trúc dữ liệu Từ điển lồng nhau trong python. Ngoài ra, chúng ta sẽ tìm hiểu về cách tạo, truy cập, sửa đổi và lặp lại thông qua chúng
Mục lục
- Từ điển là gì?
- Từ điển lồng nhau là gì?
- Các cách khác nhau để tạo Nested Dictionary
- Truy cập các phần tử trong Từ điển lồng nhau
- Thêm các mục từ điển lồng nhau
- Cập nhật các mục từ điển lồng nhau
- Hợp nhất hai từ điển lồng nhau
- Xóa các mục Từ điển lồng nhau
- Lặp lại thông qua Từ điển lồng nhau
Từ điển
Dictionary là cấu trúc dữ liệu python không có thứ tự và có thể thay đổi, lưu trữ ánh xạ của các khóa duy nhất tới các giá trị.
Nó cũng có thể được coi là
dictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
1 trong dictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
2 hoặc dictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
3 trong dictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
4. Ghi chú. Khóa từ điển có phân biệt chữ hoa chữ thường
dict1 ={'key1':'value1','key2':'value2','key3':'value3','key4':'value4'}
Ở trên, chúng tôi khai báo một từ điển có tên
dictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
5 bắt đầu bằng dấu ngoặc nhọn [{}], bao gồm tất cả các cặp khóa-giá trị được phân tách bằng dấu [,] và dấu hai chấm [. ] tách từng khóa khỏi giá trị của nó. [key1, key2, key3, key4] là các khóa duy nhất tương ứng và [value1, value2, value3, value4] là các giá trị tương ứng được gán cho các khóaTừ điển lồng nhau
Từ điển lồng nhau về cơ bản có nghĩa là đặt một từ điển bên trong một từ điển khác
nested_dict = {'id1':'Fred','id2':'Hughes','dict2':{'id3':'Philips','id4':'Jock'}}
Ở đây, chúng tôi thấy rằng chúng tôi có các cặp khóa-giá trị thông thường bao gồm một từ điển
dictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
6 có 2 cặp khóa-giá trị mô tả dictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
7 là Từ điển lồng nhauCác phương pháp khác nhau để tạo từ điển lồng nhau trong Python
Có hai cách để tạo Từ điển lồng nhau
- Cũng giống như từ điển thông thường, chúng ta có thể tạo từ điển lồng nhau chỉ khác ở chỗ mỗi giá trị có thể là một từ điển hoặc một cặp
8dictA = {'student1':{'address1':'Las Vegas'}, 'student2':{'address2':'Delhi'}, 'student3':{'address3':'Texas'}, 'student4':{'address4': 'Mumbai'}}
dictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
- Ngoài ra, chúng ta có thể xây dựng Nested dictionary từ constructor
9. Để làm được điều đó, chúng ta cần chuyển các cặp khóa-giá trị vào đối số từ khóadictA = {'student1':{'address1':'Las Vegas'}, 'student2':{'address2':'Delhi'}, 'student3':{'address3':'Texas'}, 'student4':{'address4': 'Mumbai'}}
9dictA = {'student1':{'address1':'Las Vegas'}, 'student2':{'address2':'Delhi'}, 'student3':{'address3':'Texas'}, 'student4':{'address4': 'Mumbai'}}
dictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
3Chúng tôi cũng có thể tạo từ điển với các giá trị mặc định cho mỗi khóa bằng phương pháp
dictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
31dictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
5Truy cập các phần tử trong một từ điển lồng nhau
Các khóa, giá trị riêng lẻ có thể được truy cập bằng cách sử dụng
dictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
32dictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
7Trên từ điển này, nếu chúng ta muốn lấy
dictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
33 của dictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
34, chúng ta sẽ sử dụng câu lệnh indictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
0Nếu có một tham chiếu đến khóa không có trong từ điển lồng nhau, thì nó sẽ tạo ra một ngoại lệ
dictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
1Đầu ra câu lệnh này sẽ là một ngoại lệ của keyerror, key sẽ không có trong thành phố có tên
dictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
35. ĐúngdictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
3Để ngăn điều này xảy ra, chúng ta có thể sử dụng phương thức từ điển đặc biệt get[].
______136 cung cấp giá trị cho khóa tương ứng của nó nếu chúng có mặt trong từ điển, nếu không, nó trả về
dictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
37, điều đó có nghĩa là phương thức này sẽ không bao giờ tăng dictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
38. Thêm các mục vào từ điển lồng nhau trong Python
Nếu chúng tôi muốn thêm mục, giá trị hoặc khóa mới vào từ điển, chúng tôi có thể thêm Từ điển liên quan đến nó
dictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
7Giả sử trong
dictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
39, chúng tôi sẽ thêm một sinh viên3, chúng tôi tiến hành nhưnested_dict = {'id1':'Fred','id2':'Hughes','dict2':{'id3':'Philips','id4':'Jock'}}
0Điều này thêm vào từ điển mới được thêm vào từ điển lồng nhau. Và bây giờ, dictA trông như thế này
nested_dict = {'id1':'Fred','id2':'Hughes','dict2':{'id3':'Philips','id4':'Jock'}}
1Ngoài ra, chúng tôi có thể nói rằng
dictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
39 đã được cập nhật vì chúng tôi đã thêm một mục vào đó. Giả sử chúng tôi muốn thay đổi bất kỳ khóa hoặc giá trị hoặc mục nào đó trong từ điển lồng nhau, chúng tôi giới thiệu mục đó theo khóa và giá trị tương ứng của nó và thay đổi nó thành những gì chúng tôi muốn.
nested_dict = {'id1':'Fred','id2':'Hughes','dict2':{'id3':'Philips','id4':'Jock'}}
1Cập nhật các mục được lưu trữ trong một từ điển lồng nhau
Giả sử sinh viên3 thay đổi địa chỉ của mình thành Paris từ Texas. Đây, làm thế nào chúng ta có thể làm điều này
nested_dict = {'id1':'Fred','id2':'Hughes','dict2':{'id3':'Philips','id4':'Jock'}}
3Bây giờ dữ liệu của
dictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
51 đã được cập nhật với địa chỉ mới dictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
52Hợp nhất hai từ điển lồng nhau trong Python
Phương thức
53 có sẵn của python có thể được sử dụng để hợp nhất hai từ điển lồng nhau [cách thực hiện là hợp nhất các khóa và giá trị của các từ điển với nhau]dictA = {'student1':{'address1':'Las Vegas'}, 'student2':{'address2':'Delhi'}, 'student3':{'address3':'Texas'}, 'student4':{'address4': 'Mumbai'}}
Một điều cần lưu ý là ở đây,
53 sẽ ghi đè giá trị của các khóa giống nhau và từ đó xung đột với nhau sẽ bị ghi đèdictA = {'student1':{'address1':'Las Vegas'}, 'student2':{'address2':'Delhi'}, 'student3':{'address3':'Texas'}, 'student4':{'address4': 'Mumbai'}}
nested_dict = {'id1':'Fred','id2':'Hughes','dict2':{'id3':'Philips','id4':'Jock'}}
4- Lưu ý, một trong những khóa
55 được nhân đôi đã bị xóa và chúng tôi chỉ có 4 từ điển trong các từ điển lồng nhau cho biết rằng một từ điển lặp lại trongdictA = {'student1':{'address1':'Las Vegas'}, 'student2':{'address2':'Delhi'}, 'student3':{'address3':'Texas'}, 'student4':{'address4': 'Mumbai'}}
56 đã bị xóadictA = {'student1':{'address1':'Las Vegas'}, 'student2':{'address2':'Delhi'}, 'student3':{'address3':'Texas'}, 'student4':{'address4': 'Mumbai'}}
Xóa các mục từ điển lồng nhau trong Python
Có nhiều cách khác nhau để loại bỏ các mục trong từ điển lồng nhau, một số trong số đó đang sử dụng phương pháp
dictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
57, v.v.- Bằng cách sử dụng key , chúng ta có thể xóa key đó và giá trị tương ứng của nó bằng phương thức
57dictA = {'student1':{'address1':'Las Vegas'}, 'student2':{'address2':'Delhi'}, 'student3':{'address3':'Texas'}, 'student4':{'address4': 'Mumbai'}}
nested_dict = {'id1':'Fred','id2':'Hughes','dict2':{'id3':'Philips','id4':'Jock'}}
5- Giả sử, chúng tôi không cần giá trị bật lên cho bất kỳ mục đích nào, chúng tôi có thể xóa nó bằng phương pháp
59dictA = {'student1':{'address1':'Las Vegas'}, 'student2':{'address2':'Delhi'}, 'student3':{'address3':'Texas'}, 'student4':{'address4': 'Mumbai'}}
nested_dict = {'id1':'Fred','id2':'Hughes','dict2':{'id3':'Philips','id4':'Jock'}}
6- Nếu chúng tôi muốn bật mục cuối cùng trong từ điển lồng nhau, chúng tôi có thể sử dụng
70 để làm điều tương tựdictA = {'student1':{'address1':'Las Vegas'}, 'student2':{'address2':'Delhi'}, 'student3':{'address3':'Texas'}, 'student4':{'address4': 'Mumbai'}}
nested_dict = {'id1':'Fred','id2':'Hughes','dict2':{'id3':'Philips','id4':'Jock'}}
7Ở đây, bây giờ chúng tôi sẽ in đầu ra của
dictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
71 và dictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
72. Chúng tôi hy vọng đầu ra là dictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
71 không bao gồm khóa dictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
74 [đồng thời giá trị tương ứng của nó cũng sẽ bị xóa] và dictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
75 sẽ là mục cuối cùng được bật có khóa dictA = {'student1':{'address1':'Las Vegas'},
'student2':{'address2':'Delhi'},
'student3':{'address3':'Texas'},
'student4':{'address4': 'Mumbai'}}
74đầu ra
nested_dict = {'id1':'Fred','id2':'Hughes','dict2':{'id3':'Philips','id4':'Jock'}}
8Lặp lại qua các từ điển lồng nhau trong python
Tất cả các giá trị trong từ điển lồng nhau có thể được lặp lại với sự trợ giúp của ________ 177 chủ yếu được lồng vào một