Làm cách nào để kiểm tra loại từ điển trong Python?

Đôi khi, chúng ta cần kiểm tra xem một từ điển cụ thể có trống hay không. Trong miền phát triển web mà đôi khi chúng tôi cần kiểm tra kết quả của một truy vấn cụ thể hoặc kiểm tra xem chúng tôi có bất kỳ khóa nào để thêm thông tin vào cơ sở dữ liệu không. Hãy thảo luận về những cách nhất định mà tác vụ này có thể được thực hiện trong Python.  

Kiểm tra xem Từ điển có trống không bằng cách sử dụng bool[]

Hàm bool có thể được sử dụng để thực hiện tác vụ cụ thể này. Như tên gợi ý, nó thực hiện nhiệm vụ chuyển đổi một đối tượng thành giá trị boolean, nhưng ở đây, việc chuyển một chuỗi trống sẽ trả về Sai, vì không thể chuyển đổi một thứ gì đó trống.  

Python3




The original dictionary : {}
Is dictionary empty ? : True
6

The original dictionary : {}
Is dictionary empty ? : True
7
The original dictionary : {}
Is dictionary empty ? : True
8
The original dictionary : {}
Is dictionary empty ? : True
9

 

The original dictionary : {}
Is dictionary empty ? : True
0

The original dictionary : {}
Is dictionary empty ? : True
1
The original dictionary : {}
Is dictionary empty ? : True
0
The original dictionary : {}
Is dictionary empty ? : True
1
The original dictionary : {}
Is dictionary empty ? : True
2
The original dictionary : {}
Is dictionary empty ? : True
3
The original dictionary : {}
Is dictionary empty ? : True
4

 

The original dictionary : {}
Is dictionary empty ? : True
5

The original dictionary : {}
Is dictionary empty ? : True
6

The original dictionary : {}
Is dictionary empty ? : True
7____48
The original dictionary : {}
Is dictionary empty ? : True
9
The original dictionary : {}
Is dictionary empty ? : True
0____01

 

The original dictionary : {}
Is dictionary empty ? : True
2

The original dictionary : {}
Is dictionary empty ? : True
1
The original dictionary : {}
Is dictionary empty ? : True
0
The original dictionary : {}
Is dictionary empty ? : True
5
The original dictionary : {}
Is dictionary empty ? : True
2
The original dictionary : {}
Is dictionary empty ? : True
3
The original dictionary : {}
Is dictionary empty ? : True
8

đầu ra

The original dictionary : {}
Is dictionary empty ? : True

Kiểm tra xem Từ điển có trống không bằng cách sử dụng toán tử not

Tác vụ này cũng có thể được thực hiện bằng cách sử dụng toán tử not để kiểm tra sự tồn tại của từ điển, điều này đánh giá là True nếu không tìm thấy bất kỳ khóa nào trong từ điển.  

Python3




The original dictionary : {}
Is dictionary empty ? : True
6

The original dictionary : {}
Is dictionary empty ? : True
7
The original dictionary : {}
Is dictionary empty ? : True
8
The original dictionary : {}
Is dictionary empty ? : True
9

 

The original dictionary : {}
Is dictionary empty ? : True
0

The original dictionary : {}
Is dictionary empty ? : True
1
The original dictionary : {}
Is dictionary empty ? : True
0
The original dictionary : {}
Is dictionary empty ? : True
1
The original dictionary : {}
Is dictionary empty ? : True
2
The original dictionary : {}
Is dictionary empty ? : True
3
The original dictionary : {}
Is dictionary empty ? : True
4

 

The original dictionary : {}
Is dictionary empty ? : True
60

The original dictionary : {}
Is dictionary empty ? : True
6

The original dictionary : {}
Is dictionary empty ? : True
7
The original dictionary : {}
Is dictionary empty ? : True
8
The original dictionary : {}
Is dictionary empty ? : True
9
The original dictionary : {}
Is dictionary empty ? : True
7

 

The original dictionary : {}
Is dictionary empty ? : True
2

The original dictionary : {}
Is dictionary empty ? : True
1
The original dictionary : {}
Is dictionary empty ? : True
0
The original dictionary : {}
Is dictionary empty ? : True
5
The original dictionary : {}
Is dictionary empty ? : True
2
The original dictionary : {}
Is dictionary empty ? : True
3
The original dictionary : {}
Is dictionary empty ? : True
8

đầu ra

The original dictionary : {}
Is dictionary empty ? : True

Kiểm tra xem Từ điển có trống không bằng cách sử dụng len[]

Ở đây, chúng tôi đang sử dụng Python len[] để kiểm tra xem từ điển có trống hay không

Python3




The original dictionary : {}
Is dictionary empty ? : True
6

The original dictionary : {}
Is dictionary empty ? : True
7
The original dictionary : {}
Is dictionary empty ? : True
8
The original dictionary : {}
Is dictionary empty ? : True
9

 

The original dictionary : {}
Is dictionary empty ? : True
0

The original dictionary : {}
Is dictionary empty ? : True
1
The original dictionary : {}
Is dictionary empty ? : True
0
The original dictionary : {}
Is dictionary empty ? : True
1
The original dictionary : {}
Is dictionary empty ? : True
2
The original dictionary : {}
Is dictionary empty ? : True
3
The original dictionary : {}
Is dictionary empty ? : True
4

 

The original dictionary : {}
Is dictionary empty ? : True
84

The original dictionary : {}
Is dictionary empty ? : True
7
The original dictionary : {}
Is dictionary empty ? : True
8
The original dictionary : {}
Is dictionary empty ? : True
87____488
The original dictionary : {}
Is dictionary empty ? : True
8
The original dictionary : {}
Is dictionary empty ? : True
8
The original dictionary : {}
Is dictionary empty ? : True
91

 

The original dictionary : {}
Is dictionary empty ? : True
2

The original dictionary : {}
Is dictionary empty ? : True
1
The original dictionary : {}
Is dictionary empty ? : True
0
The original dictionary : {}
Is dictionary empty ? : True
5
The original dictionary : {}
Is dictionary empty ? : True
2
The original dictionary : {}
Is dictionary empty ? : True
3
The original dictionary : {}
Is dictionary empty ? : True
8

đầu ra

The original dictionary : {}
Is dictionary empty ? : True

Kiểm tra xem Từ điển có trống không bằng cách sử dụng Toán tử đẳng thức

Ở đây, chúng tôi đang so sánh từ điển có giá trị với từ điển trống để kiểm tra xem từ điển có trống hay không

Python3




The original dictionary : {}
Is dictionary empty ? : True
6

The original dictionary : {}
Is dictionary empty ? : True
00
The original dictionary : {}
Is dictionary empty ? : True
8
The original dictionary : {}
Is dictionary empty ? : True
02
The original dictionary : {}
Is dictionary empty ? : True
03
The original dictionary : {}
Is dictionary empty ? : True
04
The original dictionary : {}
Is dictionary empty ? : True
05
The original dictionary : {}
Is dictionary empty ? : True
06
The original dictionary : {}
Is dictionary empty ? : True
07
The original dictionary : {}
Is dictionary empty ? : True
04
The original dictionary : {}
Is dictionary empty ? : True
09
The original dictionary : {}
Is dictionary empty ? : True
10

Có loại từ điển nào trong Python không?

Python cung cấp một loại dữ liệu tổng hợp khác được gọi là từ điển , tương tự như danh sách ở chỗ nó là một tập hợp các đối tượng. Đây là những gì bạn sẽ học trong hướng dẫn này. Bạn sẽ bao gồm các đặc điểm cơ bản của từ điển Python và tìm hiểu cách truy cập và quản lý dữ liệu từ điển.

Các loại từ điển trong Python là gì?

Có 4 loại từ điển trong Python. .
Ra lệnhDict
Mặc địnhDict
Bản Đồ Chuỗi
Phản đối

Kiểu dữ liệu từ điển là gì?

Đại diện cho một bộ sưu tập khóa và giá trị không có thứ tự . Kiểu dữ liệu Từ điển được tối ưu hóa để tra cứu nhanh các giá trị.

Phương pháp __ dict __ là gì?

Tất cả đối tượng trong Python đều có thuộc tính __dict__, là đối tượng từ điển chứa tất cả các thuộc tính được xác định cho chính đối tượng đó . Việc ánh xạ các thuộc tính với các giá trị của nó được thực hiện để tạo từ điển.

Chủ Đề