提问人:begadali 提问时间:1/5/2023 最后编辑:begadali 更新时间:1/5/2023 访问量:76
遍历可变长度字典的嵌套列表,获取特定的键值对
looping through nested list of dictionary of variable length to get particular key value pair
问:
list1=[{'value': [{'children': [{'children': [{'children': [{'children': [{'children': [{'coupling': {'category': 'A', 'value': '0'}, 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/controller/IncentiveRateController.java', 'metric': 'coupling', 'name': 'IncentiveRateController.java'}, {'coupling': {'category': 'A', 'value': '0'}, 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/controller/MaintainIncentiveController.java', 'metric': 'coupling', 'name': 'MaintainIncentiveController.java'}], 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/controller', 'name': 'controller'}, {'children': [{'coupling': {'category': 'A', 'value': '0'}, 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/delegate/IncentiveRateDelegate.java', 'metric': 'coupling', 'name': 'IncentiveRateDelegate.java'}], 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/delegate', 'name': 'delegate'}, {'children': [{'coupling': {'category': 'A', 'value': '0'}, 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/impl/IncentiveRateJPARepositoryImpl.java', 'metric': 'coupling', 'name': 'IncentiveRateJPARepositoryImpl.java'}], 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/impl', 'name': 'impl'}], 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src', 'name': 'src'}], 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv', 'name': 'vvv'}], 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz', 'name': 'xyz'}], 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271', 'name': '89224495-b8b3-43c9-ac8d-addb755ab271'}]}]
必填列表是
list2=[
{'category': 'A', 'value': '0', 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/controller/IncentiveRateController.java', 'metric': 'coupling', 'name': 'IncentiveRateController.java'},
{'category': 'A', 'value': '0', 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/controller/MaintainIncentiveController.java', 'metric': 'coupling', 'name': 'MaintainIncentiveController.java'},
{'category': 'A', 'value': '0', 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/delegate/IncentiveRateDelegate.java', 'metric': 'coupling', 'name': 'IncentiveRateDelegate.java'},
{'category': 'A', 'value': '0', 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/impl/IncentiveRateJPARepositoryImpl.java', 'metric': 'coupling', 'name': 'IncentiveRateJPARepositoryImpl.java'}]
上面的列表是 Pandas DataFrame 中的一列
答:
0赞
Surjit Samra
1/5/2023
#1
创建一个递归函数,该函数可以更深入地访问包含比键更高一级的字典,在这种情况下,“耦合”是必需的,在列表中捕获这些字典,而在递归中,该列表将使您的数据:)
def deep(data, key,l):
if isinstance(data, list):
for d in data:
deep(d, key,l)
if isinstance(data, dict):
if key in data.keys():
l.append(data)
#return data
else:
for dkey in data.keys():
if isinstance(data.get(dkey), dict):
deep(data.get(dkey), key,l)
if isinstance(data.get(dkey), list):
for d in data.get(dkey):
deep(d, key,l)
else:
continue
return l #you must return list
l=[]
deep(list1,'coupling',l)
l
Out[161]:
[{'coupling': {'category': 'A', 'value': '0'},
'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/controller/IncentiveRateController.java',
'metric': 'coupling',
'name': 'IncentiveRateController.java'},
{'coupling': {'category': 'A', 'value': '0'},
'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/controller/MaintainIncentiveController.java',
'metric': 'coupling',
'name': 'MaintainIncentiveController.java'},
{'coupling': {'category': 'A', 'value': '0'},
'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/delegate/IncentiveRateDelegate.java',
'metric': 'coupling',
'name': 'IncentiveRateDelegate.java'},
{'coupling': {'category': 'A', 'value': '0'},
'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/impl/IncentiveRateJPARepositoryImpl.java',
'metric': 'coupling',
'name': 'IncentiveRateJPARepositoryImpl.java'}]
在 df 上申请
l=[]
df = pd.DataFrame([list1],columns=['A'])
df['B']=df['A'].apply(lambda x: deep(x,'coupling',l))
df['B']
Out[133]:
0 [{'coupling': {'category': 'A', 'value': '0'}, 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/controller/IncentiveRateController.java', 'metric': 'coupling', 'name': 'IncentiveRateController.java'}, {'coupling': {'category': 'A', 'value': '0'}, 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/controller/MaintainIncentiveController.java', 'metric': 'coupling', 'name': 'MaintainIncentiveController.java'}, {'coupling': {'category': 'A', 'value': '0'}, 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/delegate/IncentiveRateDelegate.java', 'metric': 'coupling', 'name': 'IncentiveRateDelegate.java'}, {'coupling': {'category': 'A', 'value': '0'}, 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/impl/IncentiveRateJPARepositoryImpl.java',...
Name: B, dtype: object
评论
0赞
begadali
1/5/2023
如果我想将此函数应用于 DataFrame 中的列,我应该怎么做?apply 方法
评论