没有必要将数字转换为浮点,因为它们都是整数。
在分号上拆分每一行。第一个标记是公司名称(带有星号前缀,我们将删除它)。剩下的代币都是需要加在一起的数字。
建立一个元组(收入、公司)列表,然后可以对其进行排序,以确定收入最高的公司。
这样地:
FILENAME = "foo.txt"
def company_with_max_revenue(filename):
data = []
with open(filename) as f:
for row in f:
company, *revenues = row.split(";")
sr = sum(map(int, revenues))
data.append((sr, company[1:]))
return sorted(data)[-1]
revenue, company = company_with_max_revenue(FILENAME)
print(f"The biggest revenue: {company} The revenue is {revenue}")
输出
The biggest revenue: Company 3 The revenue is 34000