Python 从多深度字典中绘制元素
假设一个多深度dictPython 从多深度字典中绘制元素,python,Python,假设一个多深度dict {'definition': 'dirname', 'get': ['getatime', 'getctime', 'getmtime', 'getsize'], 'operation': ['join', {'split':['split', 'splitdrive', 'splitext']}, {'expand': ['expanduser', 'expandvars']},
{'definition': 'dirname',
'get': ['getatime', 'getctime', 'getmtime', 'getsize'],
'operation': ['join',
{'split':['split', 'splitdrive', 'splitext']},
{'expand': ['expanduser', 'expandvars']},
],
}
我试图用定义的函数提取它的项
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
过于复杂的递归函数解决了这个问题
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)
一行或多行摘要,无需声明变量和使用全局。从
字典中获取所有元素的递归代码
def traverse(d):
for k, v in d.items():
if isinstance(v, dict):
traverse(v)
else:
print(k,':',v)
#traverse(d)
#驱动程序值
IN :
d = {'definition': 'dirname',
'get': ['getatime', 'getctime', 'getmtime', 'getsize'],
'operation': ['join',
{'split': ['split', 'splitdrive', 'splitext']},
{'expand': ['expanduser', 'expandvars']}
]
}
OUT :
definition : dirname
get : ['getatime', 'getctime', 'getmtime', 'getsize']
operation : ['join',
{'split': ['split', 'splitdrive', 'splitext']},
{'expand': ['expanduser', 'expandvars']}
]
编辑:根据规范,要从字典中获取所有值,一般递归方法:
l=[]
def traverse(d):
for k, v in d.items():
print(k,':',v)
if type(v)==list: #since may be a list
for ele in v: #access the list
if type(ele) == dict: #since list may contain another dict
traverse(ele)
else: #normal values inside the list
l.append(ele)
continue
elif isinstance(v, dict): #if the value is a dict
traverse(v)
else: #normal value
l.append(v)
traverse(d)
print(l)
#驱动程序值:
OUT: l = ['dirname',
'getatime',
'getctime',
'getmtime',
'getsize',
'join',
'split',
'splitdrive',
'splitext',
'expanduser',
'expandvars']
我认为您的解决方案很好,因为您希望从字典和数组中提取特定数据
稍微短一点的版本可能是这样的,但我认为它仍然不像人们希望的那样“优雅”:
def count_d(dt):
lst = []
dt_values = dt.values() if isinstance(dt, dict) else dt
for value in dt_values:
if isinstance(value, str):
lst.append(value)
else:
lst.extend(count_d2(value))
return lst
此函数的优点是不需要创建全局数组,因此可以调用:
data = {'definition': 'dirname',
'get': ['getatime', 'getctime', 'getmtime', 'getsize'],
'operation': ['join',
{'split':['split', 'splitdrive', 'splitext']},
{'expand': ['expanduser', 'expandvars']},
],
}
print(count_d(data))
# ['dirname', 'join', 'split', 'splitdrive', 'splitext', 'expanduser', 'expandvars', 'getatime', 'getctime', 'getmtime', 'getsize']
使用regex
import re
j=[]
y1 = re.compile("(?<=')[^']+(?='[,|\]$])")
for value in y1.findall(str(dt)):
j.append(value)
print(j)
未能将它们包含在一个列表中。只需将值附加到另一个列表中
更复杂的~嗯,这有多复杂???我将更改并发布整个代码。@DFK,已编辑,但我认为我不需要这样做。您能准确地编写您想要的输出吗?它只是一个字符串数组,所有关键信息都丢失了:['dirname'、'getatime'、'getctime'、'getmtime'、'getsize'、'join'、'split'、'splitdrive'、'splitext'、'expanduser'、'expandvars']Yes@LukasAnsteegIs输出列表的顺序正确很重要吗?
['dirname', 'getatime', 'getctime', 'getmtime', 'getsize', 'join', 'split', 'splitdrive', 'splitext', 'expanduser', 'expandvars']