|
本帖最后由 Finleysion 于 2015-8-31 09:48 编辑
[size=18.6667px]
[size=18.6667px]最近在学习python编程,可能是从C转过来还不太习惯,想实现一个复杂的结构体,但是呢,运行结果老是不正确,希望高人指教。
class Data():
def __init__(self):
Data.longitude=0
Data.latitude=0
Data.strength=0
class data_unit():
def __init__(self):
data_unit.info=[]
data_unit.fre=0
data_unit.info_records=0
def add_info(self,data):
x=id(self.info)
if isinstance(data,Data):
newobj=copy.deepcopy(Data())
data_unit.info.append(newobj)
data_unit.info_records+=1
else:
print("Wrong instance was put into data container!")
return
def del_info(self,index):
if self.info_recods==0:
print("There's no data to del already!")
return
if index>self.info_recods:
print("Error! Index was out of range!")
else:
self.info.remove(index)
self.info_recods-=1
returnclass Data_Container():
def __init__(self):
Data_Container.container=[]
Data_Container.data_set_records=0
def add_data(self,new_data):
if isinstance(new_data,data_unit):
newobj=copy.deepcopy(new_data)
Data_Container.container.append(newobj)
Data_Container.data_set_records+=1
del newobj
else:
print("Wrong instance was put into data container!")
return
def del_data(self,index):
if self.data_set_records==0:
print("There's no data set can be delete already!")
return
if index>self.data_set_records:
print("Error! Index was out of range!")
return
else:
self.container.remove(index)
self.data_set_records-=1
return
def data_inlet(self,data):
if isinstance(data,Date_lead_in):
if self.data_set_records==0:
unit=data_unit()
unit.fre=data.fre
datax=Data()
datax=copy.deepcopy(data.data)
unit.info=unit.add_info(datax)
self.add_data(unit)
return
for i in range(0,self.data_set_records):
if data.fre==self.container.fre:
newobj=Data()
newobj=copy.deepcopy(data.data) self.container.add_info(newobj)
break
elif self.data_set_records==i:
unit=data_unit()
unit.fre=data.fre
unit.info=unit.add_info(data.data)
self.add_data(unit)
class Date_lead_in():
data=Data()
fre=0
if __name__=='__main__':
container=Data_Container()
for j in range(0,4):
datas=data_unit()
datas.fre=j
for i in range(0,10):
data=Data()
data.longitude=j+1
data.latitude=j+2
data.strength=3+j
datas.add_info(data)
container.add_data(datas)
程序有点长,抱歉,整体上说就是需要一个容器来装很多个分类,Data_Container就是这个容器,data_unit就是分类的单元,然后分类单元里面又有很多组数据,并且这些数据的多少事先都是不可知的,所以想借用列表的数据动态增加的功能。但是在实际使用的过程中,Data_Container里面的分类里的列表info[]都指向了一个分类的空间,这个是为什么呢?每一个分类里面的info[]都指向一个独立的内存空间,这应该怎么实现呢?
|
|