0
我有下面的代碼Python中添加對象的for循環
old_county_code = -1
old_placement_id = -1
placements = []
for row in raw_data: #raw_data is one line from the database retrieved by cursor.fetchall()
placement_id = row[1]
if placement_id != old_placement_id:
placement = Objects.placement()
placement.placement_id = placement_id
placements.append(placement)
country_code = row[3]
if old_county_code != country_code:
country = Objects.country()
country.country_id = country_code
placement.countries.append(country)
creative = Objects.creative(row[2], row[0], row[4], row[5], row[6], row[7])
country.creatives.append(creative)
old_placement_id = placement_id
old_county_code = country_code
對象配置包含本身包含廣告的列表國家名單。因此,當我運行此代碼時,我注意到每個展示位置都包含與列表對象placement.countries中包含的國家對象完全相同的數量。事實上,情況並非如此。我認爲我在代碼中做了一些錯誤,但我不知道是什麼。
這是對象的代碼
class placement(object):
placement_id = 0
countries = []
class country(object):
country_id = 0
creatives = []
class creative(object):
creative_id = 0
matching_id = 0
clicks = 0
impressions = 0
ctr = 0.0
rank = 0.0
不知道你的努力從您的文章做......做一個小例子(非常小),並顯示所有輸入和所有輸出然後把你的預期輸出 –
另請參見[這個問題](http://stackoverflow.com/questions/8860447/why-does-python-seem-to-treat -instance變量-作爲共享對象之間-)。這是一個「類與實例」變量問題。 – DSM
http://sscce.org/ – Marcin