Đô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 ? : True6
The original dictionary : {} Is dictionary empty ? : True7
The original dictionary : {} Is dictionary empty ? : True8
The original dictionary : {} Is dictionary empty ? : True9
The original dictionary : {} Is dictionary empty ? : True0
The original dictionary : {} Is dictionary empty ? : True1
The original dictionary : {} Is dictionary empty ? : True0
The original dictionary : {} Is dictionary empty ? : True1
The original dictionary : {} Is dictionary empty ? : True2
The original dictionary : {} Is dictionary empty ? : True3
The original dictionary : {} Is dictionary empty ? : True4
The original dictionary : {} Is dictionary empty ? : True5
The original dictionary : {} Is dictionary empty ? : True6
The original dictionary : {} Is dictionary empty ? : True7____48
The original dictionary : {} Is dictionary empty ? : True9
The original dictionary : {} Is dictionary empty ? : True0____01
The original dictionary : {} Is dictionary empty ? : True2
The original dictionary : {} Is dictionary empty ? : True1
The original dictionary : {} Is dictionary empty ? : True0
The original dictionary : {} Is dictionary empty ? : True5
The original dictionary : {} Is dictionary empty ? : True2
The original dictionary : {} Is dictionary empty ? : True3
The original dictionary : {} Is dictionary empty ? : True8
đầ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 ? : True6
The original dictionary : {} Is dictionary empty ? : True7
The original dictionary : {} Is dictionary empty ? : True8
The original dictionary : {} Is dictionary empty ? : True9
The original dictionary : {} Is dictionary empty ? : True0
The original dictionary : {} Is dictionary empty ? : True1
The original dictionary : {} Is dictionary empty ? : True0
The original dictionary : {} Is dictionary empty ? : True1
The original dictionary : {} Is dictionary empty ? : True2
The original dictionary : {} Is dictionary empty ? : True3
The original dictionary : {} Is dictionary empty ? : True4
The original dictionary : {} Is dictionary empty ? : True60
The original dictionary : {} Is dictionary empty ? : True6
The original dictionary : {} Is dictionary empty ? : True7
The original dictionary : {} Is dictionary empty ? : True8
The original dictionary : {} Is dictionary empty ? : True9
The original dictionary : {} Is dictionary empty ? : True7
The original dictionary : {} Is dictionary empty ? : True2
The original dictionary : {} Is dictionary empty ? : True1
The original dictionary : {} Is dictionary empty ? : True0
The original dictionary : {} Is dictionary empty ? : True5
The original dictionary : {} Is dictionary empty ? : True2
The original dictionary : {} Is dictionary empty ? : True3
The original dictionary : {} Is dictionary empty ? : True8
đầ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 ? : True6
The original dictionary : {} Is dictionary empty ? : True7
The original dictionary : {} Is dictionary empty ? : True8
The original dictionary : {} Is dictionary empty ? : True9
The original dictionary : {} Is dictionary empty ? : True0
The original dictionary : {} Is dictionary empty ? : True1
The original dictionary : {} Is dictionary empty ? : True0
The original dictionary : {} Is dictionary empty ? : True1
The original dictionary : {} Is dictionary empty ? : True2
The original dictionary : {} Is dictionary empty ? : True3
The original dictionary : {} Is dictionary empty ? : True4
The original dictionary : {} Is dictionary empty ? : True84
The original dictionary : {} Is dictionary empty ? : True7
The original dictionary : {} Is dictionary empty ? : True8
The original dictionary : {} Is dictionary empty ? : True87____488
The original dictionary : {} Is dictionary empty ? : True8
The original dictionary : {} Is dictionary empty ? : True8
The original dictionary : {} Is dictionary empty ? : True91
The original dictionary : {} Is dictionary empty ? : True2
The original dictionary : {} Is dictionary empty ? : True1
The original dictionary : {} Is dictionary empty ? : True0
The original dictionary : {} Is dictionary empty ? : True5
The original dictionary : {} Is dictionary empty ? : True2
The original dictionary : {} Is dictionary empty ? : True3
The original dictionary : {} Is dictionary empty ? : True8
đầ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 ? : True6
The original dictionary : {} Is dictionary empty ? : True00
The original dictionary : {} Is dictionary empty ? : True8
The original dictionary : {} Is dictionary empty ? : True02
The original dictionary : {} Is dictionary empty ? : True03
The original dictionary : {} Is dictionary empty ? : True04
The original dictionary : {} Is dictionary empty ? : True05
The original dictionary : {} Is dictionary empty ? : True06
The original dictionary : {} Is dictionary empty ? : True07
The original dictionary : {} Is dictionary empty ? : True04
The original dictionary : {} Is dictionary empty ? : True09
The original dictionary : {} Is dictionary empty ? : True10