编程语言
首页 > 编程语言> > Python - Get leaves count in a dictionary

Python - Get leaves count in a dictionary

作者:互联网

def get_dict_leaves_count(dic):
    if len(dic) == 0:
        return 0
    else:
        cnt = 0
        for k, v in dic.items():
            if not isinstance(v, Mapping):
                cnt += 1
            else:
                cnt += get_dict_leaves_count(v)
        return cnt

 

标签:count,cnt,return,get,Python,leaves,dic
来源: https://www.cnblogs.com/zhangzhihui/p/15837976.html