Hurry! Write an interface to find the number of descendants of a particular element Linux in the dictionary structure?

< H2 > question < / H2 >

write an interface to find the number of descendants of a specific element Linux in the dictionary structure data
[recursion should be used, known result should be 7]

unix_tree = {
            "Unix": {
                "PWB/Unix": {
                    "System III": {
                        "HP-UX": None
                    },
                    "System V": {
                        "UnixWare": None,
                        "Solaris": {
                            "OpenSolaris": None
                        }
                    }
                },
                "BSD": {
                    "Unix 9": None,
                    "FreeBSD": None,
                    "NetBSD": None,
                    "MacOS": None
                },
                "Xenix": {
                    "Sco Unix": {
                        "OpenServer": None
                    },
                    "AIX": None,
                },
            },
            "Linux": {
                "Debian": {
                    "Ubuntu": None,
                    "Linux Mint": None
                },
                "Redhat": {
                    "CentOS": None,
                    "Fedora": None
                },
                "Gentoo": None
            }
        }


def count_of_all_distributions_of_linux(unix_tree):
    pass

topic description

sources of topics and their own ideas

related codes

/ / Please paste the code text below (do not replace the code with pictures)

what result do you expect? What is the error message actually seen?

Apr.03,2021

use the recursive method. It's very short. Look at

.
def get_dictkeys(dic):
    key_sum = 0
    for key in dic.keys():
        key_sum = key_sum+1
        if isinstance(dic[key], dict):
            key_sum = key_sum+get_dictkeys(dic[key])
    return key_sum
print get_dictkeys(unix_tree["Linux"])
Menu