绘制元件假设一个多个深度字典从多个深度字典
{'definition': 'dirname',
'get': ['getatime', 'getctime', 'getmtime', 'getsize'],
'operation': ['join',
{'split':['split', 'splitdrive', 'splitext']},
{'expand': ['expanduser', 'expandvars']},
],
}
我试图与一个definded函数来提取其项目。
lst = []
def count_d(dt):
global lst # global is unnecessary here
if isinstance(dt, dict):
for value in dt.values():
if isinstance(value, str):
lst.append(value)
else:
count_d(value)
elif isinstance(dt,list):
for ele in dt:
if isinstance(ele, str):
lst.append(ele)
else:
count_d(ele)
return lst
over_complicated递归函数解决了这个问题。
Out[121]:
['dirname',
'getatime',
'getctime',
'getmtime',
'getsize',
'join',
'split',
'splitdrive',
'splitext',
'expanduser',
'expandvars']
我所期待的是:
lst = []
def count_d(dt):
global lst
for ele in dt:
if isinstance(ele,str):
lst.append(ele)
或在一行更抽象的不声明变量,使用全局。
你能写出你想要的输出吗? 它只是一个字符串数组,丢失所有关键信息: ['dirname','getatime','getctime','getmtime','getsize','join','split','splitdrive',' splitext','expanduser','expandvars'] –
是@LukasAnsteeg –
重要的是输出列表的顺序是否正确? –