我有一个数据,我不会在给定的两个日期段内按周汇总:
我有一个tuple列表,它显示了我的transactions表中每天的销售额

Metadate = [(Day, 'totalSales By dollars')]

--
data=[('2013-06-21', 14),
 ('2013-06-20', 19),
 ('2013-06-23', 11),
 ('2013-06-22', 13),
 ('2013-06-25', 6),
 ('2013-06-24', 22),
 ('2013-06-27', 22),
 ('2013-06-26', 3),
 ('2013-06-29', 10),
 ('2013-06-28', 15),
 ('2013-04-05', 20),
 ('2013-04-04', 33),
 ('2013-04-07', 20),
 ('2013-04-06', 15),
 ('2013-04-01', 23),
 ('2013-04-03', 5),
 ('2013-04-02', 8),
 ('2013-04-09', 10),
 ('2013-04-08', 24),
 ('2013-05-08', 1),
 ('2013-05-09', 29),
 ('2013-05-02', 17),
 ('2013-05-03', 18),
 ('2013-05-01', 4),
 ('2013-05-06', 16),
 ('2013-05-07', 10),
 ('2013-05-04', 9),
 ('2013-05-05', 12),
 ('2013-05-19', 21),
 ('2013-05-18', 26),
 ('2013-05-11', 8),
 ('2013-05-10', 12),
 ('2013-05-13', 24),
 ('2013-05-12', 9),
 ('2013-05-15', 5),
 ('2013-05-14', 7),
 ('2013-05-17', 20),
 ('2013-05-16', 36),
 ('2013-05-20', 24),
 ('2013-05-21', 5),
 ('2013-05-22', 3),
 ('2013-05-23', 18),
 ('2013-05-24', 8),
 ('2013-05-25', 11),
 ('2013-05-26', 9),
 ('2013-05-27', 13),
 ('2013-05-28', 4),
 ('2013-05-29', 7),
 ('2013-06-18', 9),
 ('2013-06-19', 2),
 ('2013-06-10', 20),
 ('2013-06-11', 4),
 ('2013-06-12', 3),
 ('2013-06-13', 25),
 ('2013-06-14', 16),
 ('2013-06-15', 10),
 ('2013-06-16', 11),
 ('2013-06-17', 17),
 ('2013-04-30', 12),
 ('2013-05-31', 13),
 ('2013-05-30', 29),
 ('2013-06-09', 12),
 ('2013-06-08', 20),
 ('2013-06-07', 47),
 ('2013-06-06', 5),
 ('2013-06-05', 2),
 ('2013-06-04', 3),
 ('2013-06-03', 32),
 ('2013-06-02', 13),
 ('2013-06-01', 9),
 ('2013-04-23', 3),
 ('2013-04-22', 33),
 ('2013-04-21', 14),
 ('2013-04-20', 20),
 ('2013-04-27', 15),
 ('2013-04-26', 17),
 ('2013-04-25', 21),
 ('2013-04-24', 1),
 ('2013-04-29', 34),
 ('2013-04-28', 11),
 ('2013-06-30', 13),
 ('2013-04-16', 5),
 ('2013-04-17', 3),
 ('2013-04-14', 10),
 ('2013-04-15', 22),
 ('2013-04-12', 23),
 ('2013-04-13', 19),
 ('2013-04-10', 1),
 ('2013-04-11', 31),
 ('2013-04-18', 27),
 ('2013-04-19', 14)]

我想按周汇总两个给定的开始日期和结束日期:
输出示例:
[(2013-05-07, 900), [(2013-05-14, 1800),....., (2013-08-01, 1000)]
提前谢谢!

最佳答案

试试这个:

from datetime import datetime
import itertools

sales = [
    ('2013-05-01', 100),
    ('2013-05-02', 200),
    ('2013-05-03', 150),
    ('2013-05-03', 120),
    ('2013-05-04', 200),
    ('2013-08-01', 250),
]

def toWeek(sale):
    '''(date,volume) -> date of the Sunday of that week'''
    sunday = datetime.strptime(sale[0], '%Y-%m-%d').strftime('%Y-%U-0')
    return datetime.strptime(sunday, '%Y-%U-%w').strftime('%Y-%m-%d')

grouped_sales = itertools.groupby(sales, toWeek)

aggregate_sales = (
    (week, sum(day_sales for date, day_sales in week_sales))
    for week, week_sales in grouped_sales)

print list(aggregate_sales)

08-08 01:08