python – 嵌套字典:提取叶子的路径
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 嵌套字典:提取叶子的路径,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3335字,纯文字阅读大概需要5分钟。
内容图文
![python – 嵌套字典:提取叶子的路径](/upload/InfoBanner/zyjiaocheng/721/53299bee9b184cefbc690f1eb9124362.jpg)
我有一个Python嵌套字典如下:
{'dist_river':
{'high':
{'wind_speed':
{'1':
{'population':
{'high':
{'school':
{'high':'T', 'medium':'T', 'low':'F'}
},
'medium':
{'land_cover':
{'Mix_garden':
{'income_source':
{'Plantation':'T', 'Agriculture':'F'}
}
}
}
}
}
}
},
'low': 'F'
}
}
如何从嵌套字典中获取子句?例如,来自dic的子句:
results = [
{'dist_river':
{'high':
{'wind_speed':
{'1':
{'population':
{'high':
{'school':
{'high': 'T', 'medium': 'T', 'low': 'F'}
}}}}}}},
{'dist_river':
{'high':
{'wind_speed':
{'1':
{'population':
{'medium':
{'land_cover':
{'Mix_garden':
{'income_source':
{'Plantation': 'T', 'Agriculture': 'F'}
}}}}}}}}},
{'dist_river':
{'low': 'F'}
}
]
lengths(results) == 3
谢谢您的帮助
社区编辑:似乎每个结果字典必须只有一个条目用于每个嵌套级别.换句话说,每个结果都包含字典树中每个叶子的完整路径. – Tim Pietzcker 13小时前
解决方法:
import collections
def isDict(d):
return isinstance(d, collections.Mapping)
def isAtomOrFlat(d):
return not isDict(d) or not any(isDict(v) for v in d.values())
def leafPaths(nestedDicts, noDeeper=isAtomOrFlat):
"""
For each leaf in NESTEDDICTS, this yields a
dictionary consisting of only the entries between the root
and the leaf.
"""
for key,value in nestedDicts.items():
if noDeeper(value):
yield {key: value}
else:
for subpath in leafPaths(value):
yield {key: subpath}
演示:
>>> pprint.pprint(list( leafPaths(dic) ))
[{'dist_river': {'high': {'wind_speed': {'1': {'population': {'high': {'school': {'high': 'T',
'low': 'F',
'medium': 'T'}}}}}}}},
{'dist_river': {'high': {'wind_speed': {'1': {'population': {'medium': {'land_cover': {'Mix_garden': {'income_source': {'Agriculture': 'F',
'Plantation': 'T'}}}}}}}}}},
{'dist_river': {'low': 'F'}}]
旁注1:但是,除非出于某种原因保证这种格式是合理的,否则我个人认为以元组的方式产生节点会更好,例如:就像是:
...noDeeper=lambda x:not isDict(x)...
...yield tuple(value)
...yield (key,)+subpath
[('dist_river', 'high', 'wind_speed', '1', 'population', 'high', 'school', 'high', 'T'),
('dist_river', 'high', 'wind_speed', '1', 'population', 'high', 'school', 'medium', 'T'),
('dist_river', 'high', 'wind_speed', '1', 'population', 'high', 'school', 'low', 'F'),
('dist_river', 'high', 'wind_speed', '1', 'population', 'medium', 'land_cover', 'Mix_garden', 'income_source', 'Plantation', 'T'),
('dist_river', 'high', 'wind_speed', '1', 'population', 'medium', 'land_cover', 'Mix_garden', 'income_source', 'Agriculture', 'F'),
('dist_river', 'low', 'F')]
(很容易从“直截了当”的答案中提取,这恰好是thg435的答案.)
旁注2:请注意,天真的实施不是OP正在寻找的.天真的实现将有noDeeper = lambda x:not isDict(x),结果为:
>>> pprint.pprint(list( leafPaths(dic) ))
[{'dist_river': {'high': {'wind_speed': {'1': {'population': {'high': {'school': {'high': 'T'}}}}}}}},
{'dist_river': {'high': {'wind_speed': {'1': {'population': {'high': {'school': {'medium': 'T'}}}}}}}},
{'dist_river': {'high': {'wind_speed': {'1': {'population': {'high': {'school': {'low': 'F'}}}}}}}},
{'dist_river': {'high': {'wind_speed': {'1': {'population': {'medium': {'land_cover': {'Mix_garden': {'income_source': {'Plantation': 'T'}}}}}}}}}},
{'dist_river': {'high': {'wind_speed': {'1': {'population': {'medium': {'land_cover': {'Mix_garden': {'income_source': {'Agriculture': 'F'}}}}}}}}}},
{'dist_river': {'low': 'F'}}]
编辑:这是一种效率低下的算法.每片叶片L重新产生深度(L)次.更高效的是链式生成器可能具有自定义数据结构,或手动模拟堆栈.
内容总结
以上是互联网集市为您收集整理的python – 嵌套字典:提取叶子的路径全部内容,希望文章能够帮你解决python – 嵌套字典:提取叶子的路径所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。