尝试使用python构建工具。该工具应执行以下操作:
假设我有两个字典-
dict() #1 - data
{'name_id': 'manager_id_1', 'name_first': 'manager_name_1', 'direct_report:' [{'name_id': 'employee_id_1', 'name_first': 'employee_name_1', 'direct_report': []}, {'name_id': 'employee_id_2', 'name_first': 'employee_name_2', 'direct_report': []}, {'name_id': 'employee_id_3', 'name_first': 'employee_name_3', 'direct_report': [{'name_id': 'employee_id_3_1', 'name_first': 'employee_name_3_1', 'direct_report': []}, {'name_id': 'employee_id_3_2', 'name_first': 'employee_name_3_2', 'direct_report': []}]}
dict() #2 - data_count
{'employee_id_1': 2, 'xxxx_id': 3, 'employee_id_2': 5, 'xxxx_id': 5, 'employee_id_3_2': 6}
第一个dict包含所有个人信息和报告结构,而第二个dict包含每个人的数据计数组(包括dict 1中的那些用户)
任务是对总数求和,并在新的字典中显示经理下的总数,如下所示:
{manager_name_1: 7}
{employee_name_3: 6}
这是我编写的函数,但未返回任何内容。如果删除返回结果[data_name],则会出现错误-TypeError:+不支持的操作数类型:“ int”和“ NoneType”。今天尝试调试它,但无法弄清楚我做错了什么地方,任何建议将不胜感激。
def data_recursive(data_sample, data_count_sample):
result = dict()
data_user = str
data_name = str
data_report = str
for key, value in data_sample.items():
if 'name_id' in key:
data_user = value
elif 'name_first' in key:
data_name = value
else:
data_report = value
if len(data_report) == 0:
result[data_name] = data_count_sample.get(data_user, 0)
return data_count_sample.get(data_user, 0)
else:
user_sum = 0
for data_users in data_report:
x = recusive = data_recursive(data_users, data_count_sample)
user_sum = user_sum + x
result[data_name] = data_count_sample.get(data_user, 0) + user_sum
return result[data_name]
print(result)
return(result)
答案 0 :(得分:0)
有错别字,字典格式不正确。检查更正的代码示例。
data_sample = { 'name_id': 'manager_id_1',
'name_first': 'manager_name_1',
'direct_report': [
{
'name_id': 'employee_id_1',
'name_first': 'employee_name_1',
'direct_report': []
},
{ 'name_id': 'employee_id_2',
'name_first': 'employee_name_2',
'direct_report': []
},
{ 'name_id': 'employee_id_3',
'name_first': 'employee_name_3',
'direct_report': [{
'name_id': 'employee_id_3_1',
'name_first': 'employee_name_3_1',
'direct_report': []
}]
},
{ 'name_id': 'employee_id_3_2',
'name_first': 'employee_name_3_2',
'direct_report': []
}]
}
data_count_sample = { 'employee_id_1': 2,
'xxxx_id': 3,
'employee_id_2': 5,
'xxxx_id': 5,
'employee_id_3_2': 6
}
def data_recursive(data_sample, data_count_sample):
result = dict()
data_user = str
data_name = str
for key, value in data_sample.items():
if 'name_id' in key:
data_user = value
elif 'name_first' in key:
data_name = value
else:
data_report = value
if len(data_report) == 0:
result[data_name] = data_count_sample.get(data_user, 0)
return data_count_sample.get(data_user, 0)
else:
user_sum = 0
for data_users in data_report:
x = data_recursive(data_users, data_count_sample)
user_sum = user_sum + x
result[data_name] = data_count_sample.get(data_user, 0) + user_sum
return result[data_name]
return result
output = data_recursive(data_sample, data_count_sample)
print(output)
输出
13
答案 1 :(得分:0)
我建议将结果字典作为参数传递给data_recursive()
方法。将签名更改为:
def data_recursive(data_sample, data_count_sample, result):
并删除该行:
result = dict()
然后,当您调用该方法时,请使用:
ret = dict()
data_recursive(data_sample, data_count_sample, ret)
ret
字典应该保存您的结果。
答案 2 :(得分:0)
为了获得理想的提示字典,这是我的建议:
def data_recursive(data, data_count):
name = data['name_first']
result = {name: 0}
report = data['direct_report']
if report:
result[name] = sum([data_count.get(r['name_id'], 0) for r in report])
for d in [data_recursive(reporter, data_count) for reporter in report]:
for key, value in d.items():
result[key] = value
else:
result[name] = data_count.get(data['name_id'], 0)
return result
结果
{'manager_name_1': 7,
'employee_name_1': 2,
'employee_name_2': 5,
'employee_name_3': 6,
'employee_name_3_1': 0,
'employee_name_3_2': 6}
注释