Bạn muốn xóa một từ điển trong Python? . Trong bài viết này, chúng ta sẽ tìm hiểu các Cách khác nhau để xóa cặp khóa-giá trị khỏi Từ điển Python
mệnh lệnh. clear[] để xóa từ điển trong Python
Python đã tích hợp sẵn
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 để xóa từ điển trong Python. Phương thức clear[] xóa tất cả các cặp khóa-giá trị có trong dict và trả về một dict trống
cú pháp
dict.clear[]
Ví dụ
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]]
đầu ra
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 cặp khóa-giá trị khỏi Từ điển Python
Có thể sử dụng các kỹ thuật sau để xóa cặp khóa-giá trị khỏi từ điển
- Hàm pop[] trong Python
- Từ khóa Python del
- Phương thức popitem[] được xây dựng sẵn trong Python
- Hiểu từ điển cùng với phương thức item[] của Python
1. Sử dụng phương thức pop[]
Phương thức pop[] trong Python có thể được sử dụng để xóa một khóa và một giá trị được liên kết với nó. e. một cặp khóa-giá trị từ một từ điển
cú pháp
dict.pop[key]
Phương thức
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 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 khỏi dict và trả về dict đã cập nhật
Ví dụ
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 ta đã chuyển khóa – “A” làm đối số cho phương thức pop[]. Do đó, nó xóa cặp giá trị khóa được liên kết với “A”
đầu ra
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: {}4 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ứ là 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ẻ
Từ khóa del trong Python cũng có thể được sử dụng để xóa cặp khóa-giá trị khỏi các giá trị từ điển đầu vào
cú pháp
del dict[key]
ví dụ 1
dict.clear[]0
đầu ra
dict.clear[]1
ví dụ 2. Xóa cặp khóa-giá trị khỏi Từ điển lồng nhau
cú pháp
Chúng tôi có thể xóa cặp khóa-giá trị 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
dict.clear[]2
Ví dụ
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]]0
Vì vậy, ở đây, chúng ta xóa cặp khóa-giá trị “______05” được liên kết với khóa ngoài “
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“
đầu ra
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]]1
3. Phương thức popitem[] trong Python
Hàm
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 của Python có thể được sử dụng để xóa một cặp khóa-giá trị ngẫu nhiên hoặc tùy ý khỏi lệnh Python. Hàm popitem[] không chấp nhận đối số và trả về cặp khóa-giá trị đã xóa khỏi từ điển
cú pháp
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]]2
Ví dụ
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]]3
đầu ra
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]]4
4. Python Dict Comprehension cùng với phương thức items[]
Phương thức item[] của Python cùng với Dict Comprehension có thể được sử dụng để xóa từ điển trong Python
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 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 khóa-giá trị trong từ điển cụ thể
cú pháp
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]]5
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: {}9 có thể được sử dụng để tạo từ điển bằng cách chấp nhận các cặp khóa-giá trị từ một lần lặp cụ thể
cú pháp
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]]6
Trong ngữ cảnh xóa các cặp khóa-giá trị khỏi từ điển, phương thức items[] có thể được sử dụng để cung cấp danh sách các cặp khóa-giá trị cho khả năng hiểu chính tả dưới dạng có thể lặp lại
dict.pop[key]0 được sử dụng để gặp khóa-giá trị đượ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 lệnh mới chứa tất cả các cặp khóa-giá trị ngoại trừ cặp khóa-giá trị được liên kết với khóa sẽ bị xóa
Ví dụ
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]]7
đầu ra
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]]8
Xóa một từ điển trong Python bằng cách chỉ định các phần tử trong khi lặp lại
Trong khi xử lý Từ điển Python, chúng tôi có thể gặp các tình huống trong đó chúng tôi muốn xóa một cặp khóa-giá trị trong quá trình lặp lại từ điển
Để phục vụ mục đích này, chúng ta có thể tạo danh sách từ điển đầu vào và sử dụng
dict.pop[key]1 để duyệt qua danh sách từ điển
cú pháp
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]]9
Cuối cùng, bằng cách sử dụng một
dict.pop[key]0, chúng ta sẽ kiểm tra xem vòng lặp có gặp khóa bị xóa không. Ngay khi gặp khóa, từ khóa del có thể được sử dụng để xóa cặp khóa-giá trị