所以我在更新python字典時遇到了問題。下面是字典的功能:更新功能字典
def customer_dictionary(balfilename):
d = {}
bafile = open(balfilename, 'r')
for line in bafile:
dic = line.split()
d[dic[1]] = [dic[0] , dic[2]]
return d
bafile.close()
現在我想要做的就是創建另一個函數,它看起來像:
def update_customer_dictionary(cdictionary, transfilename):
transfile = open(transfilename. 'r')
for line in transfile:
act, trans = line.strip().split()
if act in dictionary:
cdictionary[act][1] += float(trans)
else:
cdictionary[act][2] = [act][2]
我似乎無法弄清楚如何更新詞典使用這個新功能在前一個功能中做出的。 cdictionary是之前製作的字典。
File 1:
139-28-4313 115 1056.30
706-02-6945 135 -99.06
595-74-5767 143 4289.07
972-87-1379 155 3300.26
814-50-7178 162 3571.94
632-72-6766 182 3516.77
699-77-2796 191 2565.29
File 2:
380 2932.48
192 -830.84
379 2338.82
249 3444.99
466 -88.33
466 2702.32
502 -414.31
554 881.21
你能提供一個樣本,說明你的字典應該看起來像什麼嗎?另外,請顯示'update_customer_dictionary'的代碼 – idjaw
將代碼格式化爲代碼,以便更易讀 – shafeen
爲什麼不將'customer_dictionary'創建的字典作爲參數傳遞給'update_customer_dictionary',然後返回已更改的字典? –