简体   繁体   English

如何在 Python 中制作树状图?

[英]How can I make a tree diagram in Python?

不完整的树状图

This is an incomplete tree diagram I made to classify this list of animals:这是一个不完整的树状图,我用来对这个动物列表进行分类:

horse, cow, sheep, pig, dog, cat, lion, tiger, whale, dolphin, seal, penguin, ostrich, sparrow, spider, ant, bee, wasp, termite, octopus, squid马、牛、羊、猪、狗、猫、狮子、老虎、鲸鱼、海豚、海豹、企鹅、鸵鸟、麻雀、蜘蛛、蚂蚁、蜜蜂、黄蜂、白蚁、章鱼、鱿鱼

I havent finished putting in dog, tiger, lion, etc. just because the application required me to buy some subscription for more shapes and i wasn't gonna do that, but that doesn't matter cos I can visualise the rest.我还没有完成放入狗、老虎、狮子等。只是因为应用程序要求我购买更多形状的订阅,而我不会这样做,但这并不重要,因为我可以想象其余的。 My question is;我的问题是; In python code, how can I make a program that asks the user yes/no questions continuously until it can make out what animal it is out of the list.在 python 代码中,我如何制作一个程序,不断询问用户是/否问题,直到它可以确定它是什么动物不在列表中。 I can obviously do this with lots of IF statements, or with OOP and using attributes, however both solutions require me to ask EVERY single question, which would amount to quite a lot of lines of code, and it would be quite ugly.显然,我可以使用大量 IF 语句或 OOP 和使用属性来做到这一点,但是这两种解决方案都要求我问每一个问题,这将相当于相当多的代码行,而且会非常难看。 How do I make it, for example, so that if the user says that their animal is aquatic, it no longer asks any of the questions that don't apply to the animal.例如,我如何制作它,以便如果用户说他们的动物是水生的,它就不再询问任何不适用于该动物的问题。 For example:例如:

If I pick wasp, and I answer yes to the question "Is your animal a land animal?", then no to "Is your animal a mammal?", then yes to it being a carnivore and being able to fly, how do i make it so the program will only branch to those questions?如果我选择黄蜂,我对“你的动物是陆地动物吗?”的回答是肯定的,然后对“你的动物是哺乳动物吗?”的回答是肯定的,然后对它是食肉动物并且能够飞行的回答是肯定的,我该怎么做?使程序只会分支到这些问题? Basically, how do i code a tree diagram that follows the user's inputs?基本上,我如何编写遵循用户输入的树形图? (I dont need any GUI) (我不需要任何图形用户界面)

You could define the tree with a classical Node class:您可以使用经典的Node类定义树:

class Node:
    def __init__(self, name, *children):
        self.name = name
        self.children = children

tree = Node("an animal",
    Node("aquatic",
        Node("a cephalapod",
            Node("having 8 tentacles", Node("an octopus")),
            Node("having 10 tentacles", Node("a squid"))
        ), Node("a whale",
            Node("a baleen", Node("a baleen")),
            Node("not a baleen", Node("a dolphin"))
        )
    ), Node("a land animal",
        Node("a mammal",
            Node("a bird",
                Node("able to fly", Node("a sparrow")),
                Node("not able to fly",
                    Node("a piscivore", Node("a penguin")),
                    Node("not a piscivore", Node("an ostrich"))
                )
            ), Node("not a bird",
                Node("a carnivore", Node("a lion")),
                Node("a herbivore", 
                    Node("having leather", Node("a bovine")),
                    Node("not having leather", 
                        Node("having wool", Node("a sheep")),
                        Node("not having wool", Node("a pig"))
                    )
                )
            )
        ), Node("an insect",
            Node("a carnivore",
                Node("able to fly", Node("a wasp")),
                Node("not able to fly",
                    Node("having 6 legs", Node("an ant")),
                    Node("having 8 legs", Node("a spider"))
                )
            ), Node("a herbivore", 
                Node("able to fly", Node("a bee")),
                Node("not able to fly", Node("a termite"))
            )
        )
    )
)

Then the main code can continue with the question loop:然后主代码可以继续问题循环:

node = tree
while node.children:
    answer = "n"
    for child in node.children[:-1]:
        print("Is it {}? (y/n)".format(child.name))
        answer = input()
        if answer.lower() == "y":
            break
    if answer.lower() != "y":
        child = node.children[-1]
    node = child
    print("It is {}".format(node.name))

Although your tree is binary, this code foresees the possibility to have more than 2 children.尽管您的树是二元树,但此代码预见了可能有 2 个以上的孩子。 When answering no to the question that would select the first child, it will then ask the question that would lead to the second child, ...etc, until only one other child is left as possibility: it will not ask the corresponding question, since that is the only option left.当对选择第一个孩子的问题回答否时,它会问会导致第二个孩子的问题,等等,直到只剩下一个其他孩子作为可能:它不会问相应的问题,因为这是剩下的唯一选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM