我不知道如何做到这一点:我有一个list的list这样定义S:
list=[[day,type,expense],[...]];
Run Code Online (Sandbox Code Playgroud)
日和费用是int,类型是string
我需要在白天找到最大费用.一个例子:
list=[[1,'food',15],[4,'rent', 50],[1,'other',60],[8,'bills',40]]
Run Code Online (Sandbox Code Playgroud)
我需要总结当天的元素并找到费用最高的那一天.
结果应该是:
day:1, total expenses:75
这不是一个简单的默认指令吗?
import pprint
from collections import defaultdict
from operator import itemgetter
l = [[1, 'food', 15], [4, 'rent', 50], [1, 'other', 60], [8, 'bills', 40]]
d = defaultdict(int)
for item in l:
d[item[0]] += item[2]
pprint.pprint(dict(d))
print max(d.iteritems(), key=itemgetter(1))
Run Code Online (Sandbox Code Playgroud)
结果:
{1: 75, 4: 50, 8: 40}
(1, 75)
Run Code Online (Sandbox Code Playgroud)