慕尼黑5688855
我建议使用,collections.defaultdict因为您的某些计数可能为 0。这是一个选项:from collections import defaultdictdtl = ['06/01/19 12:00', '06/01/19 12:00', '06/01/19 11:00', '05/01/19 21:00', '05/01/19 17:00', '05/01/19 17:00', '05/01/19 14:00', '03/01/19 21:00', '03/01/19 17:00', '03/01/19 12:00', '03/01/19 12:00', '03/01/19 12:00', '03/01/19 12:00', '03/01/19 12:00', '03/01/19 11:00', '03/01/19 10:00', '03/01/19 10:00', '03/01/19 09:00', '03/01/19 09:00','03/01/19 09:00',]# Nested defaultdictresult = defaultdict(lambda: defaultdict(int))for date_time in dtl: date, time = date_time.split() result[date][time.split(':')[0]] += 1输出(使用pprint):defaultdict(<function <lambda> at 0x7f20d5c37c80>, {'03/01/19': defaultdict(<class 'int'>, {'09': 3, '10': 2, '11': 1, '12': 5, '17': 1, '21': 1}), '05/01/19': defaultdict(<class 'int'>, {'14': 1, '17': 2, '21': 1}), '06/01/19': defaultdict(<class 'int'>, {'12': 2, '11': 1})})如果您真的想显示0for 打印,那么我真的看不到times像我在这里所做的那样保留数组并以dict这种方式初始化您的方法。times = ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23']dtl = ['06/01/19 12:00', '06/01/19 12:00', '06/01/19 11:00', '05/01/19 21:00', '05/01/19 17:00', '05/01/19 17:00', '05/01/19 14:00', '03/01/19 21:00', '03/01/19 17:00', '03/01/19 12:00', '03/01/19 12:00', '03/01/19 12:00', '03/01/19 12:00', '03/01/19 12:00', '03/01/19 11:00', '03/01/19 10:00', '03/01/19 10:00', '03/01/19 09:00', '03/01/19 09:00','03/01/19 09:00']result = {date_time.split()[0] : {time : 0 for time in times} for date_time in dtl}for date_time in dtl: date, time = date_time.split() result[date][time.split(':')[0]] += 1输出如下:{'06/01/19': {'00': 0, '01': 0, '02': 0, '03': 0, '04': 0, '05': 0, '06': 0, '07': 0, '08': 0, '09': 0, '10': 0, '11': 1, '12': 2, '13': 0, '14': 0, '15': 0, '16': 0, '17': 0, '18': 0, '19': 0, '20': 0, '21': 0, '22': 0, '23': 0}, '05/01/19': {'00': 0, '01': 0, '02': 0, '03': 0, '04': 0, '05': 0, '06': 0, '07': 0, '08': 0, '09': 0, '10': 0, '11': 0, '12': 0, '13': 0, '14': 1, '15': 0, '16': 0, '17': 2, '18': 0, '19': 0, '20': 0, '21': 1, '22': 0, '23': 0}, '03/01/19': {'00': 0, '01': 0, '02': 0, '03': 0, '04': 0, '05': 0, '06': 0, '07': 0, '08': 0, '09': 3, '10': 2, '11': 1, '12': 5, '13': 0, '14': 0, '15': 0, '16': 0, '17': 1, '18': 0, '19': 0, '20': 0, '21': 1, '22': 0, '23': 0}}
素胚勾勒不出你
一种快速而肮脏的方法是:#!/usr/bin/env python3def convert(dt): ret = {} for elem in dt: d,t = elem.split() t = t.split(":")[0] # not a valid value if not d: pass # we inserted d already if d in ret: if t in ret[d]: ret[d][t] += 1 else: ret[d] = {'00': 0, '01': 0, '02': 0, '03': 0, '04': 0, '05': 0, '06': 0, '07': 0, '08': 0, '09': 0, '10': 0, '11': 0, '12': 0, '13': 0, '14': 0, '15': 0, '16': 0, '17': 0, '18': 0, '19': 0, '20': 0, '21': 0, '22': 0, '23': 0 } return retdtl = ['06/01/19 12:00', '06/01/19 12:00', '06/01/19 11:00', '05/01/19 21:00', '05/01/19 17:00', '05/01/19 17:00', '05/01/19 14:00', '03/01/19 21:00', '03/01/19 17:00','03/01/19 12:00', '03/01/19 12:00', '03/01/19 12:00', '03/01/19 12:00', '03/01/19 12:00', '03/01/19 11:00', '03/01/19 10:00', '03/01/19 10:00', '03/01/19 09:00','03/01/19 09:00','03/01/19 09:00']print(convert(dtl))