我是python的新手并且遇到了这部分问题。我有一个名为Pet的课程和一个名为Dogs的课程。我需要宠物宣布一个集合,以便将各种狗添加到列表,元组或字典中,或者更容易。例如
pitbull=Dog()
shepard=Dog()
guard_dog=Dog()
animal=Pet()
我不知道python是否允许将对象存储在list中。在java中我可以简单地将它们存储在arrayList中。像pet=arrayList<Dog>()
一样。我一直在搜索并且不知道如何将Dog对象添加到python中的列表,元组或字典中。请帮助任何人
答案 0 :(得分:1)
例如使用列表
pitbull=Dog()
shepard=Dog()
guard_dog=Dog()
animal=Pet()
animals = [pitbull,shepard,guard_dog]
animals.append(animal)
#do what ever you want with the list