所以我在更新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',然后返回已更改的字典? –