我希望获得容器内的所有值(比如dictionary
)。容器可以具有嵌套对象,如dictionary, list, tuples and string
值。目前我能够编写此代码以获取所有值,但它不处理列表项或对象本身就是序列的情况。
def get_all_values_in_container(search_dict):
value_set = set()
for key, value in search_dict.items():
if isinstance(value, dict):
results = get_all_values_in_container(value)
for result in results:
value_set.add(result)
elif isinstance(value, (list, tuple)):
for items in value: # Will fail if the item itself is a sequence
value_set.add(items)
else:
value_set.add(value)
return value_set
这是我的虚拟search_dict
dummy = {'#text': ['Experience with build tools such as GRADLE, ANT or MAVEN; ',
' such as ',
'. Experience with ',
' such as ',
', Subversion, or GIT. In depth knowledge of ',
' platforms Work experience with the following ',
', or ',
'. Experience with build and ',
'. Education/Certifications ',
'. '],
'area': 'RBA5000#LI SO1 Truven Health Analytics\n',
'education': {'#text': ' degree in ',
'degree': 'Bachelor s',
'major': {'#text': 'Computer Science',
'@cipcode': '11.0701',
'@code': '0402',
'@std-major': 'COMPUTER SCIENCE'}},
'interval': {'#text': ['or ', ' work experience in related field'],
'area': 'comparable'},
'skill': ['scripting languages',
'Perl, UNIX shell',
'SCM tools',
'CVS',
'Windows, UNIX',
'technologies Oracle, SQL',
'Informix; Java/J2EE; Cognos',
'deployment automation tools like Jenkins',
'preferred_skill',[{'preferred': 'Java/J2EE'}]]}
我知道这将是一个递归调用(可能是相同的函数或其他本身是递归的函数)。我该如何进一步继续
答案 0 :(得分:0)
我想出了这个解决方案。截至目前,它工作正常(至少在我的测试用例中)
def get_all_values_in_container(container):
value_set = set()
if isinstance(container, (list, tuple, set)):
for items in container:
results = get_all_values_in_container(items)
value_set.update(results)
elif isinstance(container, dict):
for key, value in container.items():
results = get_all_values_in_container(value)
value_set.update(results)
else:
value_set.add(get_processed_item(container))
return value_set
任何人都可以提出可能失败的优势(我真的很期待这一点)。