Python将对象添加到集合中

时间:2013-01-15 17:33:42

标签: python

我是python的新手并且遇到了这部分问题。我有一个名为Pet的课程和一个名为Dogs的课程。我需要宠物宣布一个集合,以便将各种狗添加到列表,元组或字典中,或者更容易。例如

pitbull=Dog()
shepard=Dog()
guard_dog=Dog()

animal=Pet()

我不知道python是否允许将对象存储在list中。在java中我可以简单地将它们存储在arrayList中。像pet=arrayList<Dog>()一样。我一直在搜索并且不知道如何将Dog对象添加到python中的列表,元组或字典中。请帮助任何人

1 个答案:

答案 0 :(得分:1)

正如Martijn Pieters所说,你应该看看python教程

例如使用列表

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