class SpreadsheetRow(object):
def __init__(self,Account1):
self.Account1=Account1
self.Account2=0
我有一個while循環填充對象列表,另一個循環填充關聯Var1:Account2的字典。但是,如果密鑰與對象的Account1匹配,我需要將該字典的值存入每個對象。如何將dict鍵與列表中的對象的屬性相關聯?
所以基本上,我有:
listofSpreadsheetRowObjects=[SpreadsheetRow1, SpreadsheetRow2, SpreadsheetRow3]
dict_var1_to_account2={1234:888, 1991:646, 90802:5443}
我已經試過這樣:
for k, v in dict_var1_to_account2.iteritems():
if k in listOfSpreadsheetRowObjects:
if self.account1=k:
self.account2=v
但是,它不工作,我懷疑這是我的第一個 「如果」 語句,因爲listOfSpreadsheetRowObjects是隻是這些對象的列表。我如何訪問每個對象的account1,以便根據需要匹配它們?
最後,我應該有以下信息三個對象: SpreadsheetRow self.Account1 =帳戶1 self.Account2 =(V從我的字典裏,如果帳戶1在我的字典中的密鑰相匹配)
您需要CL arify你的變量是什麼,'Account1value'是什麼?它應該是來自參數的「Account1」嗎?什麼是「SpreadsheetRow1」,「SpreadsheetRow2」等?他們是否是SpreadsheetRow的實例,通常以大寫字母開頭的名稱都是類。 –
編輯,希望更清楚 – user3097421
好,因爲它是一個字典,我期望'self.Account2 = dict_of_account1_to_account2 [self.Account1]'但我不明白你在哪裏把代碼,爲什麼它檢查'如果k在listOfSpreadsheetRowObjects:'那永遠不會實現... –