Bạn muốn xóa một từ điển trong Python? Từ điển Python là một cấu trúc dữ liệu được sử dụng trong Python chấp nhận các phần tử trong dạng cặp giá trị khóa. Trong bài viết này, chúng tôi sẽ hiểu các cách khác nhau để xóa một cặp giá trị khóa khỏi từ điển Python.Ways of deleting a key-value pair from a Python Dictionary. Python có Dict.clear [] để xóa một từ điển trong Python
Elements of the dict before performing the deletion operation:
{'B': 'Java', 'D': 'Javascript', 'C': 'Fortan', 'A': 'Python'}
Elements of the dict after performing the deletion operation:
{}
6 được xây dựng để xóa một từ điển trong Python. Phương thức rõ ràng [] xóa tất cả các cặp giá trị khóa có trong Dict và trả về một dict trống.
Syntax:
Example:
inp_dict = {"A":"Python","B":"Java","C":"Fortan","D":"Javascript"} print["Elements of the dict before performing the deletion operation:\n"] print[str[inp_dict]] inp_dict.clear[] print["\nElements of the dict after performing the deletion operation:"] print[str[inp_dict]]
Output:
Elements of the dict before performing the deletion operation: {'B': 'Java', 'D': 'Javascript', 'C': 'Fortan', 'A': 'Python'} Elements of the dict after performing the deletion operation: {}
Kỹ thuật để xóa một cặp giá trị khóa khỏi từ điển Python
Các kỹ thuật sau đây có thể được sử dụng để xóa một cặp giá trị khóa khỏi từ điển:
- Chức năng python pop []
- Python del Từ khóa
- Phương thức Python Python [] được xây dựng
- Từ điển Hiểu cùng với Phương thức Python [] Phương thức
1. Sử dụng phương thức pop []
Phương thức python pop [] có thể được sử dụng để xóa khóa và giá trị liên quan đến nó, tức là một cặp giá trị khóa từ một từ điển.
Syntax:
Phương pháp
Elements of the dict before performing the deletion operation: {'B': 'Java', 'D': 'Javascript', 'C': 'Fortan', 'A': 'Python'} Elements of the dict after performing the deletion operation: {}7 về cơ bản chấp nhận một khóa để bị xóa khỏi từ điển. Nó xóa khóa cũng như giá trị được liên kết với khóa từ Dict và trả về Dict được cập nhật.key to be deleted from the dictionary. It deletes the key as well as the value associated with the key from the dict and returns the updated dict.
Example:
inp_dict = {"A":"Python","B":"Java","C":"Fortan","D":"Javascript"} print["Elements of the dict before performing the deletion operation:\n"] print[str[inp_dict]] pop_item = inp_dict.pop["A"] print["\nThe deleted element:"] print[str[pop_item]] print["\nElements of the dict after performing the deletion operation:\n"] print[str[inp_dict]]
Trong đoạn mã trên, chúng tôi đã chuyển khóa - một cách khác như một đối số cho phương thức pop []. Do đó, nó xóa cặp giá trị chính được liên kết với các A A.
Output:
Elements of the dict before performing the deletion operation: {'A': 'Python', 'B': 'Java', 'C': 'Fortan', 'D': 'Javascript'} The deleted element: Python Elements of the dict after performing the deletion operation: {'B': 'Java', 'C': 'Fortan', 'D': 'Javascript'}
2. Từ khóa Python Del
Python
Elements of the dict before performing the deletion operation: {'B': 'Java', 'D': 'Javascript', 'C': 'Fortan', 'A': 'Python'} Elements of the dict after performing the deletion operation: {}8 thực sự là một từ khóa về cơ bản được sử dụng để xóa các đối tượng. Như tất cả chúng ta đều biết rằng Python coi mọi thứ như một đối tượng, đó là lý do tại sao chúng ta có thể dễ dàng sử dụng DEL để xóa một từ điển trong Python bằng cách xóa các phần tử riêng lẻ.keyword which is basically used for the deletion of objects. As we all are aware that Python considers everything as an object, that is why we can easily use del to delete a dictionary in Python by deleting elements individually.
Từ khóa Python Del cũng có thể được sử dụng để xóa một cặp giá trị khóa khỏi các giá trị từ điển đầu vào.delete a key-value pair from the input dictionary values.
Syntax:
Ví dụ 1:
inp_dict = {"A":"Python","B":"Java","C":"Fortan","D":"Javascript"} print["Elements of the dict before performing the deletion operation:\n"] print[str[inp_dict]] del inp_dict["D"] print["\nElements of the dict after performing the deletion operation:\n"] print[str[inp_dict]]
Output:
Elements of the dict before performing the deletion operation: {'A': 'Python', 'B': 'Java', 'C': 'Fortan', 'D': 'Javascript'} Elements of the dict after performing the deletion operation: {'A': 'Python', 'B': 'Java', 'C': 'Fortan'}
Ví dụ 2: Xóa một cặp giá trị khóa khỏi từ điển lồng nhau Deleting a key-value pair from a Nested Dictionary
Syntax:
Chúng tôi có thể xóa cặp giá trị khóa khỏi từ điển Python lồng nhau. Từ khóa Del phục vụ mục đích với cú pháp dưới đây:nested Python Dictionary. The del keyword serves the purpose with the below syntax:
del dict[outer-dict-key-name][key-name-associated-with-the-value]
Example:
inp_dict = {"Python":{"A":"Set","B":"Dict","C":"Tuple","D":"List"}, "1":"Java","2":"Fortan"} print["Elements of the dict before performing the deletion operation:\n"] print[str[inp_dict]] del inp_dict["Python"]["C"] print["\nElements of the dict after performing the deletion operation:\n"] print[str[inp_dict]]
Do đó, ở đây, chúng tôi xóa cặp có giá trị khóa
Elements of the dict before performing the deletion operation: {'B': 'Java', 'D': 'Javascript', 'C': 'Fortan', 'A': 'Python'} Elements of the dict after performing the deletion operation: {}9, liên kết với khóa bên ngoài ____ ____.
Output:
Elements of the dict before performing the deletion operation: {'Python': {'A': 'Set', 'B': 'Dict', 'C': 'Tuple', 'D': 'List'}, '1': 'Java', '2': 'Fortan'} Elements of the dict after performing the deletion operation: {'Python': {'A': 'Set', 'B': 'Dict', 'D': 'List'}, '1': 'Java', '2': 'Fortan'}
3. Phương thức Python Popitem []
Hàm Python
inp_dict = {"A":"Python","B":"Java","C":"Fortan","D":"Javascript"} print["Elements of the dict before performing the deletion operation:\n"] print[str[inp_dict]] pop_item = inp_dict.pop["A"] print["\nThe deleted element:"] print[str[pop_item]] print["\nElements of the dict after performing the deletion operation:\n"] print[str[inp_dict]]1 có thể được sử dụng để xóa một cặp giá trị khóa ngẫu nhiên hoặc tùy ý khỏi một dict Python. Hàm popitem [] không chấp nhận đối số và trả về cặp giá trị khóa bị xóa từ từ điển.no arguments and returns the deleted key-value pair from the dictionary.
Syntax:
Example:
inp_dict = {"A":"Python","B":"Java","C":"Fortan","D":"Javascript"} print["Elements of the dict before performing the deletion operation:\n"] print[str[inp_dict]] pop_item = inp_dict.popitem[] print["\nThe deleted element:"] print[str[pop_item]] print["\nElements of the dict after performing the deletion operation:\n"] print[str[inp_dict]]
Output:
Elements of the dict before performing the deletion operation: {'B': 'Java', 'D': 'Javascript', 'C': 'Fortan', 'A': 'Python'} Elements of the dict after performing the deletion operation: {}0
4. Python Dictriblions cùng với phương thức Items []
Phương thức Python Item [] cùng với sự hiểu biết của Dict có thể được sử dụng để xóa một từ điển trong Python.
Python
inp_dict = {"A":"Python","B":"Java","C":"Fortan","D":"Javascript"} print["Elements of the dict before performing the deletion operation:\n"] print[str[inp_dict]] pop_item = inp_dict.pop["A"] print["\nThe deleted element:"] print[str[pop_item]] print["\nElements of the dict after performing the deletion operation:\n"] print[str[inp_dict]]2 về cơ bản không có đối số và trả về một đối tượng chứa danh sách tất cả các cặp giá trị khóa trong từ điển cụ thể.
Syntax:
Python
inp_dict = {"A":"Python","B":"Java","C":"Fortan","D":"Javascript"} print["Elements of the dict before performing the deletion operation:\n"] print[str[inp_dict]] pop_item = inp_dict.pop["A"] print["\nThe deleted element:"] print[str[pop_item]] print["\nElements of the dict after performing the deletion operation:\n"] print[str[inp_dict]]3 có thể được sử dụng để tạo một từ điển bằng cách chấp nhận các cặp giá trị khóa từ một điều đáng kể cụ thể.
Syntax:
Elements of the dict before performing the deletion operation: {'B': 'Java', 'D': 'Javascript', 'C': 'Fortan', 'A': 'Python'} Elements of the dict after performing the deletion operation: {}1
Trong bối cảnh xóa các cặp giá trị khóa từ một từ điển, phương thức mục [] có thể được sử dụng để cung cấp danh sách các cặp giá trị khóa cho sự hiểu biết chính thống như là một điều đáng tin cậy.
inp_dict = {"A":"Python","B":"Java","C":"Fortan","D":"Javascript"} print["Elements of the dict before performing the deletion operation:\n"] print[str[inp_dict]] pop_item = inp_dict.pop["A"] print["\nThe deleted element:"] print[str[pop_item]] print["\nElements of the dict after performing the deletion operation:\n"] print[str[inp_dict]]4 được sử dụng để gặp giá trị quan trọng được đề cập trước nó. Nếu gặp phải giá trị khóa được đề cập, nó sẽ trả về một dict mới chứa tất cả các cặp giá trị khóa không bao gồm cặp giá trị khóa liên kết với khóa sẽ bị xóa.
Example:
Elements of the dict before performing the deletion operation: {'B': 'Java', 'D': 'Javascript', 'C': 'Fortan', 'A': 'Python'} Elements of the dict after performing the deletion operation: {}2
Output:
Elements of the dict before performing the deletion operation: {'B': 'Java', 'D': 'Javascript', 'C': 'Fortan', 'A': 'Python'} Elements of the dict after performing the deletion operation: {}3
Xóa từ điển bằng Python bằng cách chỉ định các yếu tố trong khi lặp lại
Trong khi xử lý từ điển Python, chúng ta có thể bắt gặp các tình huống trong đó chúng ta muốn một cặp giá trị chính bị xóa trong quá trình lặp của từ điển.
Để phục vụ mục đích, chúng tôi có thể tạo một danh sách từ điển đầu vào và sử dụng
inp_dict = {"A":"Python","B":"Java","C":"Fortan","D":"Javascript"} print["Elements of the dict before performing the deletion operation:\n"] print[str[inp_dict]] pop_item = inp_dict.pop["A"] print["\nThe deleted element:"] print[str[pop_item]] print["\nElements of the dict after performing the deletion operation:\n"] print[str[inp_dict]]5 để đi qua danh sách từ điển.list of the input-dictionary and use a
inp_dict = {"A":"Python","B":"Java","C":"Fortan","D":"Javascript"} print["Elements of the dict before performing the deletion operation:\n"] print[str[inp_dict]] pop_item = inp_dict.pop["A"] print["\nThe deleted element:"] print[str[pop_item]] print["\nElements of the dict after performing the deletion operation:\n"] print[str[inp_dict]]5 to traverse through the list of the dictionary.
Syntax:
Cuối cùng, bằng cách sử dụng
inp_dict = {"A":"Python","B":"Java","C":"Fortan","D":"Javascript"} print["Elements of the dict before performing the deletion operation:\n"] print[str[inp_dict]] pop_item = inp_dict.pop["A"] print["\nThe deleted element:"] print[str[pop_item]] print["\nElements of the dict after performing the deletion operation:\n"] print[str[inp_dict]]4, chúng tôi sẽ kiểm tra vòng lặp để gặp phải khóa bị xóa. Ngay sau khi gặp khóa, từ khóa DEL có thể được sử dụng để xóa cặp giá trị khóa.del keyword can be used to delete the key-value pair.
Example:
Elements of the dict before performing the deletion operation: {'B': 'Java', 'D': 'Javascript', 'C': 'Fortan', 'A': 'Python'} Elements of the dict after performing the deletion operation: {}4
Output:
Elements of the dict before performing the deletion operation: {'B': 'Java', 'D': 'Javascript', 'C': 'Fortan', 'A': 'Python'} Elements of the dict after performing the deletion operation: {}5
Sự kết luận
Do đó, chúng tôi đã tiết lộ các kỹ thuật khác nhau để xóa một cặp giá trị khóa khỏi từ điển Python.
Người giới thiệu
- Từ điển Python - Tạp chí
- Từ điển Python - Tài liệu chính thức