【发布时间】:2013-08-09 07:45:58
【问题描述】:
下面的代码从文本文件中读取(包含不同的数组) 并分解成单独的元素。 我可以很好地处理具有两个子项的数组,但不是第三个。
例如 - 这个文件可以正常工作:
('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()))
但我有一个不同的月份,有一个 third 数组元素(一个 bonus 数字),例如:
('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()))
【问题讨论】:
标签: python arrays python-3.x tuples