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)}