Pythonic方式检查空字典和空值

时间:2022-03-16 22:08:18

I am using these kind of dictionaries, they can be or totally empty like collection_a or contain a single one level nested dictionary which might be empty. It wont have more levels.

我正在使用这些字典,它们可以像collection_a一样或完全是空的,或者包含一个可能为空的单级嵌套字典。它没有更多的水平。

collection_a = {}
collection_b = {"test": {}}

print is_empty(collection_a)
print is_empty(collection_b)

def is_empty(collection):
    return not all(collection.values())

or

要么

def is_empty(collection):
    return not bool(collection.values())

Isnt there an unique way to check if a or b have values?

有没有一种独特的方法来检查a或b是否有值?

You can check all(collection_b.values()) but that wont work for collection_a where it will return True

你可以检查所有(collection_b.values())但不适用于collection_a,它将返回True

You can also check bool(collection_a.values()) but that wont work for collection_b where it will return True...

你也可以检查bool(collection_a.values()),但是对于collection_b它将不会返回True ...

Isnt there an unique way to include both cases?

是不是有一种独特的方式来包含这两种情况?

4 个解决方案

#1


6  

Test with any, since empty dicts are falsy:

用任何测试,因为空的dicts是假的:

>>> collection_a = {}
>>> collection_b = {"test": {}}
>>> any(collection_a.values())
False
>>> any(collection_b.values())
False

This assumes that the dictionary value is always a dictionary.

这假设字典值始终是字典。

#2


5  

If you want to check whether the dictionary has values, and whether all the values (if any) are "truthy", just combine your two tests:

如果要检查字典是否具有值,以及所有值(如果有)是否为“真实”,只需组合两个测试:

bool(collection) and all(collection.values())

(The bool in the first part is optional, but without it you will get an unintuitive {} if the dictionary is empty.)

(第一部分中的bool是可选的,但如果没有它,如果字典为空则会得到一个不直观的{}。)

Of course, if you only want to check whether any of the values in the collection are "truthy" (this is not entirely clear from your question), all you have to do is any(collection), as already stated in other answers; this will at the same time also check whether the collection is non-empty.

当然,如果您只想检查集合中的任何值是否“真实”(这在您的问题中并不完全清楚),您所要做的就是任何(集合),如其他答案中所述;这将同时检查集合是否为非空。

#3


1  

Because :

因为:

>>> dict={}
>>> not dict
True

So You can check like this :

所以你可以这样检查:

collection_a = {"hello":1,"bye":2}
if collection_a:
    #do your stuff
    print("not empty")

collection_a = {}
if collection_a:
    print("dict is not empty")

if you want to check dict a or dict b then :

如果你想检查dict a或dict b那么:

if collection_a or collection_b:
    print("not empty")

#4


0  

Alternatively, bool() in combination with more-itertools' collapse():

或者,bool()与more-itertools的collapse()组合:

import more_itertools

def has_value(thing):
    return bool(*more_itertools.collapse(thing))

>>> has_value({})
False
>>> has_value({''})
False
>>> has_value({0:['']})
False
>>> has_value({'test':[]})
True

#1


6  

Test with any, since empty dicts are falsy:

用任何测试,因为空的dicts是假的:

>>> collection_a = {}
>>> collection_b = {"test": {}}
>>> any(collection_a.values())
False
>>> any(collection_b.values())
False

This assumes that the dictionary value is always a dictionary.

这假设字典值始终是字典。

#2


5  

If you want to check whether the dictionary has values, and whether all the values (if any) are "truthy", just combine your two tests:

如果要检查字典是否具有值,以及所有值(如果有)是否为“真实”,只需组合两个测试:

bool(collection) and all(collection.values())

(The bool in the first part is optional, but without it you will get an unintuitive {} if the dictionary is empty.)

(第一部分中的bool是可选的,但如果没有它,如果字典为空则会得到一个不直观的{}。)

Of course, if you only want to check whether any of the values in the collection are "truthy" (this is not entirely clear from your question), all you have to do is any(collection), as already stated in other answers; this will at the same time also check whether the collection is non-empty.

当然,如果您只想检查集合中的任何值是否“真实”(这在您的问题中并不完全清楚),您所要做的就是任何(集合),如其他答案中所述;这将同时检查集合是否为非空。

#3


1  

Because :

因为:

>>> dict={}
>>> not dict
True

So You can check like this :

所以你可以这样检查:

collection_a = {"hello":1,"bye":2}
if collection_a:
    #do your stuff
    print("not empty")

collection_a = {}
if collection_a:
    print("dict is not empty")

if you want to check dict a or dict b then :

如果你想检查dict a或dict b那么:

if collection_a or collection_b:
    print("not empty")

#4


0  

Alternatively, bool() in combination with more-itertools' collapse():

或者,bool()与more-itertools的collapse()组合:

import more_itertools

def has_value(thing):
    return bool(*more_itertools.collapse(thing))

>>> has_value({})
False
>>> has_value({''})
False
>>> has_value({0:['']})
False
>>> has_value({'test':[]})
True