有人可以幫我做功能第一列,第二列和總第三列。使用itertools.groupby總結報告
from itertools import groupby
from operator import itemgetter
things = [('2009-09-02','j', 12),
('2009-09-02','j', 3),
('2009-09-03','k',10),
('2009-09-03','k',4),
('2009-09-03','u', 22),
('2009-09-06','m',33)]
for k, items in groupby(things, itemgetter(1)):
for subitem in items:
print(subitem)
得到了這樣的結果:
('2009-09-02', 'j', 12) ('2009-09-02', 'j', 3) ('2009-09-03', 'k', 10) ('2009-09-03', 'k', 4) ('2009-09-03', 'u', 22) ('2009-09-06', 'm', 33)
期待這樣的結果:
('2009-09-02', 'j', 15) ('2009-09-03', 'k', 14) ('2009-09-03', 'u', 22) ('2009-09-06', 'm', 33)
===================== ================================================== =
sales = [('Scotland', 'Edinburgh', 20000),
('Scotland', 'Glasgow', 12500),
('Wales', 'Cardiff', 29700),
('Wales', 'Bangor', 12800),
('England', 'London', 90000),
('England', 'Manchester', 45600),
('England', 'London', 29700)]
真棒ozgur再次感謝 –