Trong Python, các phương thức
print[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
3 và print[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
4 của từ điển print[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
5 có thể được sử dụng để thực hiện các thao tác thiết lập trên các cặp khóa và khóa-giá trị. Ví dụ: bạn có thể tạo một từ điển bao gồm các thành phần [khóa và giá trị] chung cho nhiều từ điểnBài viết này mô tả các nội dung sau
- Các phương pháp
3 vàprint[list[d1.keys[]]] # ['a', 'b', 'c'] print[type[d1.keys[]]] # print[list[d1.items[]]] # [['a', 1], ['b', 2], ['c', 3]] print[type[d1.items[]]] #
4 củaprint[list[d1.keys[]]] # ['a', 'b', 'c'] print[type[d1.keys[]]] # print[list[d1.items[]]] # [['a', 1], ['b', 2], ['c', 3]] print[type[d1.items[]]] #
5print[list[d1.keys[]]] # ['a', 'b', 'c'] print[type[d1.keys[]]] # print[list[d1.items[]]] # [['a', 1], ['b', 2], ['c', 3]] print[type[d1.items[]]] #
- Trích xuất các khóa chung cho nhiều từ điển. Giao lộ
9print[list[d1.keys[]]] # ['a', 'b', 'c'] print[type[d1.keys[]]] # print[list[d1.items[]]] # [['a', 1], ['b', 2], ['c', 3]] print[type[d1.items[]]] #
- Trích xuất tất cả các khóa từ nhiều từ điển. Liên minh
0intersection_keys = d1.keys[] & d2.keys[] print[intersection_keys] # {'c', 'b'}
- Trích xuất các khóa chỉ chứa trong một trong nhiều từ điển. Chênh lệch đối xứng
1intersection_keys = d1.keys[] & d2.keys[] print[intersection_keys] # {'c', 'b'}
Sử dụng hai từ điển sau đây làm ví dụ
d1 = {'a': 1, 'b': 2, 'c': 3}
d2 = {'b': 2, 'c': 4, 'd': 5}
nguồn.
Liên kết được tài trợ
Các phương pháp print[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
3 và print[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
4 của print[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
5
print[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
print[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
print[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
Từ điển có các phương thức
print[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
3 và print[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
4- Lặp lại từ điển [khóa và giá trị] với vòng lặp for trong Python
print[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
3 trả về dạng xem của các khóa, print[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
4 trả về dạng xem của các bộ khóa-giá trị intersection_keys = d1.keys[] & d2.keys[]
print[intersection_keys]
# {'c', 'b'}
9print[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
nguồn.
Chúng lần lượt là
print[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
90 và print[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
91 và hỗ trợ các thao tác tập hợp như print[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
92- Đặt các hoạt động trong Python [hợp, giao, chênh lệch đối xứng, v.v. ]
Từ điển cũng có một phương thức
print[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
93 trả về dạng xem các giá trị, nhưng các thao tác đặt không được hỗ trợ vì các giá trị có thể trùng nhauSau đây, các ví dụ về thao tác tập hợp sử dụng phương pháp
print[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
3 và print[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
4 được hiển thịTrích xuất các khóa chung cho nhiều từ điển. Giao lộ print[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
9
print[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
Các khóa chung cho nhiều từ điển có thể được trích xuất bằng phương thức
print[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
3 và toán tử print[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
9intersection_keys = d1.keys[] & d2.keys[]
print[intersection_keys]
# {'c', 'b'}
nguồn.
Kết quả của một hoạt động thiết lập là loại
print[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
92. Điều tương tự áp dụng cho các ví dụ sauprint[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
9nguồn.
Trong trường hợp của
print[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
4, các mục có cả khóa và giá trị chung được trích xuất. Các mục chỉ có khóa hoặc chỉ có giá trị chung bị loại trừprint[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
2nguồn.
Bạn có thể tạo một từ điển mới bằng cách chuyển một tập hợp các bộ dữ liệu
intersection_keys = d1.keys[] & d2.keys[]
print[intersection_keys]
# {'c', 'b'}
9 [= kết quả của hoạt động đã thiết lập của print[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
4] tới print[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
23print[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
6nguồn.
Liên kết được tài trợ
Trích xuất tất cả các khóa từ nhiều từ điển. Liên minh intersection_keys = d1.keys[] & d2.keys[]
print[intersection_keys]
# {'c', 'b'}
0
intersection_keys = d1.keys[] & d2.keys[]
print[intersection_keys]
# {'c', 'b'}
Tất cả các khóa trong nhiều từ điển, nghĩa là các khóa có trong ít nhất một trong nhiều từ điển, có thể được trích xuất bằng toán tử
intersection_keys = d1.keys[] & d2.keys[]
print[intersection_keys]
# {'c', 'b'}
0print[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
9nguồn.
Trong trường hợp của
print[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
4, các phần tử có khóa chung nhưng giá trị khác nhau được trích xuất riêngprint[list[d1.keys[]]]
# ['a', 'b', 'c']
print[type[d1.keys[]]]
#
print[list[d1.items[]]]
# [['a', 1], ['b', 2], ['c', 3]]
print[type[d1.items[]]]
#
1nguồn.
Như trong ví dụ này, nếu có các phần tử có khóa chung nhưng giá trị khác nhau, thì chỉ một trong số chúng được giữ lại khi tạo từ điển từ tập hợp. Không thể chỉ định giá trị nào sẽ được giữ lại