1 回答
TA贡献1815条经验 获得超6个赞
这不是您问题的答案。我只是尝试解决它并使用了不同的方法。作为参考可能有用。
tree3 = {'name':'GAS','grade':0.8,'children':[{'name':'CSA','grade':0.5,'children':[{'name':'MB','grade':0.1},{'name':'TA','grade':0.6}]},{'name':'IIW','grade':0.9,'children':[None,{'name':'IP','grade':0.99}]}]}
def count_name(entity):
count = 0
name = 'name'
# print('\n')
# print(count, entity)
if type(entity) == dict:
count += sum([key == name for key in entity.keys()])
for value in entity.values():
# print(count, value)
if type(value) == list:
count += sum([count_name(member) for member in value])
return count
count_name(tree3)
添加回答
举报