用戶定義的類的名單上有:聲明在python
class Cluster:
# some attributes
c = Cluster()
其中c
應該是類型的列表Cluster()
能否定義類似c= Cluster[]
?
我是python的初學者。
用戶定義的類的名單上有:聲明在python
class Cluster:
# some attributes
c = Cluster()
其中c
應該是類型的列表Cluster()
能否定義類似c= Cluster[]
?
我是python的初學者。
澄清:有沒有這樣的東西作爲「類型的列表Cluster
」。有一個list
類型的列表,它可以包含任何東西。
所以,你需要創建一堆Cluster
S和它們添加到常規列表:
例如與列表理解:
N = 10 # length
clusters = [Cluster() for _ in range(N)]
或使用常規循環:
clusters = []
for _ in range(N): clusters.append(Cluster())
在任何情況下,你可以在以後修改列表,因爲它只是一個普通的列表。
只要寫c = []
,然後添加Cluster
對象的list
ç這樣的:
c = []
obj = Cluster()
c += [obj]
在Python中,你不必(也不能)告訴你的列表包含什麼。因此,只要空單
c=[]
,比電話
c.append(thing)
的東西你想要的:東西可以是int類型,集羣,漂浮等。在結束的,你可以有
c=[Cluster(...),1,"2",["a",1.2],(2,3),{1,2,3}]
(其中{1,2,3}是一個集合,(2,3)是一個元組)。
非常感謝,這工作。 – akritaag 2013-04-20 21:47:14