Đối với vòng lặp trống từ điển 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 truyề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

Bạn có thể tạo một từ điển trống bằng Python không?

Cách tạo Từ điển rỗng trong Python. Để tạo một từ điển trống, trước tiên hãy tạo một tên biến sẽ là tên của từ điển. Sau đó, gán biến cho một tập hợp trống các dấu ngoặc nhọn, {}. Một cách khác để tạo từ điển rỗng là sử dụng hàm dict[] mà không truyền bất kỳ đối số nào.

Bạn có thể lặp qua từ điển Python không?

Bạn có thể lặp qua từ điển bằng cách sử dụng vòng lặp for . Khi lặp qua một từ điển, giá trị trả về là các khóa của từ điển, nhưng cũng có các phương thức để trả về các giá trị.

Từ điển Python có thể có các giá trị trống không?

Từ điển Python trống có nghĩa là nó không chứa bất kỳ phần tử cặp khóa-giá trị nào . Để tạo một từ điển trống trong ví dụ này, chúng ta có thể sử dụng một dict[], không có đối số. Nếu không có đối số nào được cung cấp, một từ điển rỗng sẽ được tạo.

Từ điển có thể trống không?

Trả lời. Có, có một số cách để kiểm tra xem từ điển có trống không. Phương pháp Python chuẩn hơn là sử dụng đánh giá Boolean trên từ điển. Một từ điển trống [hoặc các vùng chứa khác] sẽ đánh giá là Boolean Sai .

Chủ Đề