下面的代碼從文本文件(包含不同的數組) 中讀取並分解爲單獨的元素。 我有它與兩個子項目的數組,但沒有第三個正常工作。讀取具有多個項目的數組(在兩個項目上工作而不是三個)
例如 - 該文件正常工作:
('January', 2, [('curly', 30), ('larry',10), ('moe',20)])
。
staff = dict()
for item in filecontent:
month = filecontent[0]
section = filecontent[1]
for name, hours in filecontent[2]:
staff[name] = hours
print ("month:" + month)
print ("section: " + str (section))
print ("".join("%s has worked %s hours\n" % (name, hours) for name, hours in staff.items()))
overtime = int(input ("Enter overtime figure: "))
print ("".join("%s has now worked %s hours \n" % (name, (hours + overtime)) for name, hours in staff.items()))
但是我有一個不同的月份與第三數組元素(一個獎金圖),例如:
('February', 2, [('curly', 30, **10**), ('larry',10, **10**), ('moe',20, **10**)])
我在適應上述代碼嘗試是如下,但不限工作...
staff = dict()
for item in filecontent:
month = filecontent[0]
section = filecontent[1]
for name, hours, bonus in filecontent[2]:
staff[name] = hours, bonus
print ("month:" + month)
print ("section: " + str (section))
print ("".join("%s has worked %s hours with %s bonus \n" % (name, hours, bonus) for name, hours, bonus in staff.items()))