我創建了一個類distance_neighbor
,其中一個屬性是類Crime
的對象列表。
這是我從數據庫查詢結果中獲得的所有屬性的值。
如何在python中爲類的屬性設置列表值
起初,我已經設置data_Crime
清單作爲對distance_neighbor
類屬性**Crime
的價值,我用德爾清除使用後data_Crime列表,從而使data_Crime
列表可以在下一循環使用。
這是我的代碼:
conn = psycopg2.connect("dbname='Chicago_crime' user='postgres' host='localhost' password='1234'")
cur= conn.cursor()
minDistance=float(input("Nilai minimum distance : "))
cur.execute("""SELECT id_objek1, objek1, id_objek2, objek2, distance from tb_distance1 where distance<'%f'""" %(minDistance))
class Crime:
def __init__(self, id_jenis, jenis):
self.id_jenis=id_jenis
self.jenis=jenis
class distance_neighbor (Crime):
def __init__(self, distance, **Crime):
self.distance = distance
self.Crime = Crime
data_Crime =[]
data_distance = []
for id_objek1, objek1, id_objek2, objek2, distance in cur.fetchall():
data_Crime.append(Crime(id_objek1,objek1))
data_Crime.append(Crime(id_objek2,objek2))
data_distance.append(distance_neighbor(distance, data_Crime))
del data_Crime[:]
錯誤消息:
data_distance.append(distance_neighbor(distance, data_Crime))
TypeError: __init__() takes exactly 2 arguments (3 given)
我一直在使用下面回答你們修好了我的代碼,謝謝