列表列表中的datetime对象 - 如何获取每天的总数?

时间:2015-09-26 23:56:45

标签: python list datetime

我有一个字典,下面以列表显示前3个值:

day_counts_list = day_counts.values()
day_counts_list[:3]

[[(datetime.date(2015, 5, 23), 815),
  (datetime.date(2015, 5, 24), 700),
  (datetime.date(2015, 5, 25), 614),
  (datetime.date(2015, 5, 26), 2395),
  (datetime.date(2015, 5, 27), 2723),
  (datetime.date(2015, 5, 28), 2543),
  (datetime.date(2015, 5, 29), 2123)],
 [(datetime.date(2015, 5, 23), 1343),
  (datetime.date(2015, 5, 24), 1039),
  (datetime.date(2015, 5, 25), 1145),
  (datetime.date(2015, 5, 26), 2470),
  (datetime.date(2015, 5, 27), 2578),
  (datetime.date(2015, 5, 28), 2439),
  (datetime.date(2015, 5, 29), 2065)],
 [(datetime.date(2015, 5, 23), 1387),
  (datetime.date(2015, 5, 24), 1292),
  (datetime.date(2015, 5, 25), 1449),
  (datetime.date(2015, 5, 26), 2023),
  (datetime.date(2015, 5, 27), 2155),
  (datetime.date(2015, 5, 28), 2284),
  (datetime.date(2015, 5, 29), 2354)]]

我想列出每天的总数。有什么想法吗?

谢谢。

3 个答案:

答案 0 :(得分:2)

使用常规字典:

settings.primitiveProperty

使用totals = {} for group in your_list: for date, count in group: if date not in totals: totals[date] = count else: totals[date] += count print totals

collections.defaultdict(int)

答案 1 :(得分:2)

由于每个子列表都有唯一的“键”,因此这可以是一行的(除了导入):

from collections import Counter

total_day_counts = sum(map(Counter, map(dict, day_counts_list)), Counter())

在Python 3.3+中,您可以通过利用与普通Counter一起使用的就地添加的新支持来显着优化它(以避免创建这么多中间dict个对象)只创建和更新了一个Counter,而不是每个输入两个:

from collections import Counter
from functools import reduce
from operator import iadd

total_day_counts = reduce(iadd, map(dict, day_counts_list), Counter())

在Python 3.5的测试中,第一个解决方案(具有更多临时中间体)的时间大约是同一输入的第二个解决方案的2-3倍(并且sum胜过其他等效的reduce(operator.add, ...)一小部分但一致的余量可能是由于C)中的约简操作中的sum硬编码步骤。

答案 2 :(得分:0)

    ll = [[(datetime.date(2015, 5, 23), 815),
  (datetime.date(2015, 5, 24), 700),
  (datetime.date(2015, 5, 25), 614),
  (datetime.date(2015, 5, 26), 2395),
  (datetime.date(2015, 5, 27), 2723),
  (datetime.date(2015, 5, 28), 2543),
  (datetime.date(2015, 5, 29), 2123)],
 [(datetime.date(2015, 5, 23), 1343),
  (datetime.date(2015, 5, 24), 1039),
  (datetime.date(2015, 5, 25), 1145),
  (datetime.date(2015, 5, 26), 2470),
  (datetime.date(2015, 5, 27), 2578),
  (datetime.date(2015, 5, 28), 2439),
  (datetime.date(2015, 5, 29), 2065)],
 [(datetime.date(2015, 5, 23), 1387),
  (datetime.date(2015, 5, 24), 1292),
  (datetime.date(2015, 5, 25), 1449),
  (datetime.date(2015, 5, 26), 2023),
  (datetime.date(2015, 5, 27), 2155),
  (datetime.date(2015, 5, 28), 2284),
  (datetime.date(2015, 5, 29), 2354)]]

def count_total_for_date(times_list):

    """
    compute total for each date in datetime.date list
    :param times_list:
    :return:
    """

    from operator import itemgetter
    from itertools import groupby

     # flatten datime list
    li = [l for el in times_list for l in el]

    # sort by date
    li = sorted(li, key=itemgetter(0))

    # group by date
    fl = [(k,list(v)) for k,v in groupby(li,key=itemgetter(0))]

    # remove first element, that is the date, repeating itself
    clean_list = [ el[1] for el in fl]

    # this will select data for the date
    dat = lambda x : x[1]

    # create date and total sum for the date tuple
    li_of_sums =  [(x[0][0],sum(map(dat,x))) for x in clean_list]

    return li_of_sums


for d in list(count_total_for_date(ll)):
    print(d)

<强>输出

    (datetime.date(2015, 5, 23), 3545)
    (datetime.date(2015, 5, 24), 3031)
    (datetime.date(2015, 5, 25), 3208)
    (datetime.date(2015, 5, 26), 6888)
    (datetime.date(2015, 5, 27), 7456)
    (datetime.date(2015, 5, 28), 7266)
    (datetime.date(2015, 5, 29), 6542)