Extract set of leaf values found in nested dicts and lists excluding None(提取在嵌套字典和列表中找到的一组叶值,不包括无)
问题描述
我有一个从 YAML 读取的嵌套结构,它由嵌套列表和/或嵌套字典组成,或者在不同嵌套级别上混合使用.可以假设该结构不包含任何递归对象.
I have a nested structure read from YAML which is composed of nested lists and/or nested dicts or a mix of both at various levels of nesting. It can be assumed that the structure doesn't contain any recursive objects.
如何仅从中提取叶值?另外,我不想要任何 None
值.叶值包含我只关心的字符串.使用递归是可以的,考虑到结构的最大深度不足以超过堆栈递归限制.生成器也可以选择.
How do I extract from it the leaf values only? Also, I don't want any None
value. The leaf values contain strings which is all I care for. It's okay for recursion to be used, considering that the maximum depth of the structure is not large enough to exceed stack recursion limits. A generator would optionally also be fine.
存在处理扁平列表或字典的类似问题,但不是两者的混合.或者,如果扁平化一个 dict,它们也会返回扁平化的我并不真正需要的键,并冒着名称冲突的风险.
There exist similar questions which deal with flattening lists or dicts, but not a mix of both. Alternatively, if flattening a dict, they also return the flattened keys which I don't really need, and risk name conflicts.
我尝试了 more_itertools.collapse
但它的例子只显示它适用于嵌套列表,而不是字典和列表的混合.
I tried more_itertools.collapse
but its examples only show it to work with nested lists, and not with a mix of dicts and lists.
预期产出
推荐答案
这是对 参考答案 的改编以供使用一个内部函数和一个 set
.它还使用递归来为问题中包含的样本输入生成预期输出.它避免通过整个调用堆栈传递每个叶子.
This is an adaption of the reference answer to use an inner function and a single set
. It also uses recursion to produce the expected outputs for the sample inputs included in the question. It avoids passing every leaf through the entire call stack.
这篇关于提取在嵌套字典和列表中找到的一组叶值,不包括无的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!