替换从dict中删除空值,在python中递归列出
我编写了以下代码,用于删除所有空内容。这个脚本很好用,但我认为它一点也不“pytonish”。python是如何做到这一点的替换从dict中删除空值,在python中递归列出,python,list,dictionary,recursion,Python,List,Dictionary,Recursion,我编写了以下代码,用于删除所有空内容。这个脚本很好用,但我认为它一点也不“pytonish”。python是如何做到这一点的 def remove_empty_values(ret): #ret is assumed eitehr dict or list #recursively remove all key or list items which values is Null or empty string if type(ret) == list:
def remove_empty_values(ret):
#ret is assumed eitehr dict or list
#recursively remove all key or list items which values is Null or empty string
if type(ret) == list:
empty_indexes=[]
i = 0
for index in ret:
if (ret[i] == None) or ((type(ret[i]) == str and ret[i] == '') or ((type(ret[i]) == unicode and ret[i] == '') or (( type(ret[i])==dict or type(ret[i])==list ) and len(ret[i]) ==0))):
empty_indexes.append(i)
else:
if (type(ret[i])==dict or type(ret[i])==list):
remove_empty_values(ret[i])
i = i + 1
for index in empty_indexes:
del ret[index]
elif type(ret) == dict:
empty_keys=[]
for key in ret:
if (ret[key] == None) or ((type(ret[key]) == str and ret[key] == '') or ((type(ret[key]) == unicode and ret[key] == '') or (( type(ret[key])==dict or type(ret[key])==list ) and len(ret[key]) ==0))):
empty_keys.append(key)
else:
if (type(ret[key])==dict or type(ret[key])==list):
remove_empty_values(ret[key])
for key in empty_keys:
del ret[key]
如果使用Python 3.8+,则可以使用赋值表达式
:=
:
例如:
d = {'a': [{'b': 'x', 'c': ''}], 'c': ['', '1', None], 'd': ['']}
def remove(i):
if isinstance(i, dict):
return {k: vv for k, v in i.items() if (vv:=remove(v))}
elif isinstance(i, list):
return [vv for v in i if (vv:=remove(v))]
return i
print(remove(d))
印刷品:
{'a': [{'b': 'x'}], 'c': ['1']}
{'a': [{'b': 'x'}], 'c': ['1'], 'd': [0, False]}
编辑:要保留零和
False
:
d = {'a': [{'b': 'x', 'c': ''}], 'c': ['', '1', None], 'd': ['', 0, False]}
def remove(i):
if isinstance(i, dict):
return {k: vv for k, v in i.items() if (vv:=remove(v)) or v is False or v == 0}
elif isinstance(i, list):
return [vv for v in i if (vv:=remove(v)) or v is False or v == 0]
return i
print(remove(d))
印刷品:
{'a': [{'b': 'x'}], 'c': ['1']}
{'a': [{'b': 'x'}], 'c': ['1'], 'd': [0, False]}
如果使用Python 3.8+,则可以使用赋值表达式
:=
:
例如:
d = {'a': [{'b': 'x', 'c': ''}], 'c': ['', '1', None], 'd': ['']}
def remove(i):
if isinstance(i, dict):
return {k: vv for k, v in i.items() if (vv:=remove(v))}
elif isinstance(i, list):
return [vv for v in i if (vv:=remove(v))]
return i
print(remove(d))
印刷品:
{'a': [{'b': 'x'}], 'c': ['1']}
{'a': [{'b': 'x'}], 'c': ['1'], 'd': [0, False]}
编辑:要保留零和
False
:
d = {'a': [{'b': 'x', 'c': ''}], 'c': ['', '1', None], 'd': ['', 0, False]}
def remove(i):
if isinstance(i, dict):
return {k: vv for k, v in i.items() if (vv:=remove(v)) or v is False or v == 0}
elif isinstance(i, list):
return [vv for v in i if (vv:=remove(v)) or v is False or v == 0]
return i
print(remove(d))
印刷品:
{'a': [{'b': 'x'}], 'c': ['1']}
{'a': [{'b': 'x'}], 'c': ['1'], 'd': [0, False]}
你认为它不是“PythONISH”吗?检查<代码>文件()/< > ScottHunter,它的任务太长了,for循环,i= i+1,我在C++中开发,我觉得这是一个C风格代码:你认为它不是“PythONISH”?检查<代码>文件()/<代码> @ ScottHunter,它的任务太长了,for循环,i=i+1,我在C++中开发,我觉得这是一个C风格的代码:)但是这会杀死布尔错误值,不是吗?我需要保持0和显式布尔错误值,但是这会杀死布尔错误值,不是吗?我需要保持0和显式布尔假值。