ZhangZhihui's Blog  
def get_dict_leaves_count(dic):
    """
    If value of a key is a list, count all items in the list as leaves.
    :param dic: 
    :return: 
    """
    if len(dic) == 0:
        return 0
    else:
        cnt = 0
        for k, v in dic.items():
            if not isinstance(v, dict):
                if not isinstance(v, list):
                    cnt += 1
                else:
                    cnt += len(v)
            else:
                cnt += get_dict_leaves_count(v)
        return cnt

 

posted on 2022-01-24 00:25  ZhangZhihuiAAA  阅读(17)  评论(0编辑  收藏  举报