Làm cách nào để xóa và xóa các phần tử khỏi chương trình Python từ điển?

Từ điển là một Python là một thùng chứa duy trì ánh xạ của các khóa duy nhất tới các giá trị theo cách không có thứ tự và có thể thay đổi. Giá trị dữ liệu được lưu trữ trong khóa. cặp giá trị sử dụng từ điển. Từ điển được viết bằng dấu ngoặc nhọn và có khóa và giá trị

Từ điển hiện được đặt hàng kể từ Python 3. 7. Từ điển trong Python 3. 6 trở về trước không được sắp xếp

Ví dụ

Ví dụ này cho thấy hoạt động của một từ điển trong python

thisdict = { "companyname": "Tutorialspoint", "tagline" : "simplyeasylearning", } print(thisdict)

đầu ra

Đoạn mã trên tạo ra các kết quả sau

{'companyname': 'Tutorialspoint', 'tagline': 'simplyeasylearning'}

Trong bài viết này, chúng tôi loại bỏ tất cả các thành phần của từ điển trong python bằng các phương pháp khác nhau

Sử dụng phương thức clear()

Bằng cách sử dụng phương thức rõ ràng, chúng tôi loại bỏ tất cả các thành phần của từ điển. Phương thức clear() không nhận bất kỳ đối số nào và không trả về bất kỳ giá trị nào

Ví dụ

Đây là đoạn mã ví dụ sau, để xóa tất cả các thành phần của từ điển bằng phương thức clear()

this_dict = { "companyname": "Tutorialspoint", "tagline" : "simplyeasylearning", } print("Dictonary before removing elements:") print(this_dict) this_dict.clear() print("Dictionary after removing elements from it") print(this_dict)

đầu ra

Dictonary before removing elements:
{'companyname': 'Tutorialspoint', 'tagline': 'simplyeasylearning'}
Dictionary after removing elements from it
{}

Bằng cách gán lại cho một từ điển trống

Đây là cách dễ nhất để loại bỏ tất cả các thành phần của từ điển. Ở đây chúng tôi gán lại từ điển cho một từ điển trống và khi làm như vậy, tất cả các thành phần của từ điển sẽ bị mất. Ví dụ sau đây cho thấy hoạt động của phương pháp này

this_dict = { "companyname": "Tutorialspoint", "tagline" : "simplyeasylearning", } print("Dictonary before removing elements:") print(this_dict) this_dict={} print("Dictionary after removing elements from it") print(this_dict)

đầu ra

Sản lượng được tạo ra như sau

Dictonary before removing elements:
{'companyname': 'Tutorialspoint', 'tagline': 'simplyeasylearning'}
Dictionary after removing elements from it
{}

Sử dụng del và một vòng lặp

Trong phương pháp này, chúng tôi lặp lại từng phần tử của từ điển bằng vòng lặp for. Và mỗi phần tử này được xóa khỏi từ điển bằng toán tử del

ví dụ 1

Trong ví dụ này, chúng ta sẽ xem cách xóa tất cả các thành phần của từ điển bằng phương thức del và vòng lặp

Bạn có thể xóa các thành phần từ điển riêng lẻ hoặc xóa toàn bộ nội dung của từ điển. Bạn cũng có thể xóa toàn bộ từ điển trong một thao tác

Để xóa hoàn toàn toàn bộ từ điển, chỉ cần sử dụng câu lệnh del 

Ví dụ

Sau đây là một ví dụ đơn giản -

Bản thử trực tiếp

#!/usr/bin/python
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
del dict['Name']; # remove entry with key 'Name'
dict.clear(); # remove all entries in dict
del dict ; # delete entire dictionary
print "dict['Age']: ", dict['Age']
print "dict['School']: ", dict['School']

đầu ra

Điều này tạo ra kết quả sau. Lưu ý rằng một ngoại lệ được đưa ra bởi vì sau khi từ điển del dict không còn tồn tại nữa -

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

________số 8_______

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 sẽ 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

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))

đầu ra

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 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

del dict[outer-dict-key-name][key-name-associated-with-the-value]

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 tôi xóa cặp khóa-giá trị “_______7_______5” được liên kết với khóa bên 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 một 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ị

Làm cách nào để xóa và xóa các phần tử khỏi từ điển trong Python?

Từ khóa del có thể được sử dụng để xóa tại chỗ khóa có trong từ điển trong Python.

Phương pháp nào được sử dụng để loại bỏ toàn bộ phần tử của từ điển?

Sử dụng phương thức clear() . Phương thức clear() không nhận bất kỳ đối số nào và không trả về bất kỳ giá trị nào.