Python清除字典中值为空的键值对

Python清除字典中值为空的键值对

#清除值为空的键值对,但保留值为0的

def clean_empty(d):
    if not isinstance(d, (dict, list)):
        return d
    if isinstance(d, list):
        return [v for v in (clean_empty(v) for v in d) if (v or v == 0)]
    return {k: v for k, v in ((k, clean_empty(v)) for k, v in d.items()) if (v or v == 0)}
hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » Python清除字典中值为空的键值对