重构dict的pythonic递归过滤器
现在我有一段代码如下:重构dict的pythonic递归过滤器,python,dictionary,recursion,refactoring,Python,Dictionary,Recursion,Refactoring,现在我有一段代码如下: def checker(dict_element, dict_filter): global counter counter = 0 def fullfill_filter(key, value): if key in dict_filter: if value in dict_filter[key]: return True def extract_dict(di
def checker(dict_element, dict_filter):
global counter
counter = 0
def fullfill_filter(key, value):
if key in dict_filter:
if value in dict_filter[key]:
return True
def extract_dict(dict_element):
global counter
for key, value in dict_element.items():
if isinstance(value, str):
if fullfill_filter(key, value):
counter += 1
elif isinstance(value, dict):
extract_dict(value)
elif isinstance(value, list):
extract_list(key, value)
def extract_list(key, list_element):
global counter
for el in list_element:
if isinstance(el, dict):
extract_dict(el)
if isinstance(el, list):
extract_list(el)
if isinstance(el, str):
if fullfill_filter(key, el):
counter += 1
extract_dict(dict_element)
if counter == len(dict_filter.keys()):
return True
return False
list = [
{
'services': [],
'location': {
'latitude': '51.026',
'country': 'Germany',
'longitude': '13.725'
},
'available': 'true',
'technologies': ['Virtual Machines', 'Distributed Systems', 'Internet', 'Wired'],
'type': 'node',
'exclusive': 'false',
'types': [{
'disk_images': [{
'version': 'f22',
'os': 'Linux',
'name': 'Fedora f22'
}],
}]
}, {
'services': [],
'location': {
'latitude': 'unknown',
'country': 'Taiwan',
'longitude': 'unknown'
},
'available': 'false',
'technologies': ['Virtual Machines', 'Distributed Systems', 'Internet',],
'type': 'node',
'exclusive': 'false',
'types': [{
'disk_images': [{
'version': 'f24',
'os': 'Linux',
'name': 'Fedora f24'
}],
}]
}
]
它用于过滤字典dict\u元素
包含各种类型的对象,我想根据dict\u过滤器中的内容找到正确的对象
例如,我有一个字典,有两个子字典,如下所示:
def checker(dict_element, dict_filter):
global counter
counter = 0
def fullfill_filter(key, value):
if key in dict_filter:
if value in dict_filter[key]:
return True
def extract_dict(dict_element):
global counter
for key, value in dict_element.items():
if isinstance(value, str):
if fullfill_filter(key, value):
counter += 1
elif isinstance(value, dict):
extract_dict(value)
elif isinstance(value, list):
extract_list(key, value)
def extract_list(key, list_element):
global counter
for el in list_element:
if isinstance(el, dict):
extract_dict(el)
if isinstance(el, list):
extract_list(el)
if isinstance(el, str):
if fullfill_filter(key, el):
counter += 1
extract_dict(dict_element)
if counter == len(dict_filter.keys()):
return True
return False
list = [
{
'services': [],
'location': {
'latitude': '51.026',
'country': 'Germany',
'longitude': '13.725'
},
'available': 'true',
'technologies': ['Virtual Machines', 'Distributed Systems', 'Internet', 'Wired'],
'type': 'node',
'exclusive': 'false',
'types': [{
'disk_images': [{
'version': 'f22',
'os': 'Linux',
'name': 'Fedora f22'
}],
}]
}, {
'services': [],
'location': {
'latitude': 'unknown',
'country': 'Taiwan',
'longitude': 'unknown'
},
'available': 'false',
'technologies': ['Virtual Machines', 'Distributed Systems', 'Internet',],
'type': 'node',
'exclusive': 'false',
'types': [{
'disk_images': [{
'version': 'f24',
'os': 'Linux',
'name': 'Fedora f24'
}],
}]
}
]
如果我对列表中的dict执行检查程序(dict,dict_filter={'Available':'true','country':'Germany'),
或[列表中dict的检查程序(dict,{'version':'f22'})]
或[清单中的dict的checkert(dict,{technologies:'Wired'})]
,我可以过滤掉第二种情况,得到第一个dict,但是,正如你所看到的,代码很难看,命名也很糟糕(不是母语为英语的人),有没有更好更干净的方法用pythonic的方式重构它?你的dict示例无效。你是说dict_element=[…]而不是{…}?@ThomasChristensen抱歉,现在应该没问题了