3 回答

TA贡献1833条经验 获得超4个赞
一旦按日期拆分为字典,就可以将 aCounter与 a结合起来defaultdict非常有效地执行此操作。所以首先按日期拆分:
from collections import Counter, defaultdict
dtd = defaultdict(list)
for date, time in (item.split() for item in dtl):
dtd[date].append(time[:2])
现在您可以轻松地计算现有项目,并使用它们来初始化一个defaultdict将在缺失时间返回零的值:
for key in dtd:
dtd[key] = defaultdict(int, Counter(dtd[key]))
结果是:
defaultdict(list, {
'03/01/19': defaultdict(int, {
'09': 3,
'10': 2,
'11': 1,
'12': 5,
'17': 1,
'21': 1
}),
'05/01/19': defaultdict(int, {'14': 1, '17': 2, '21': 1}),
'06/01/19': defaultdict(int, {'11': 1, '12': 2})
})
由于这里的对象是defaultdicts,您将能够查询不在原始数据集中的日期和时间。您可以通过dict在完成后将结果转换为仅包含您想要的键的正则来避免这种情况:
hours = ['%02d' % h for h in range(24)]
dtd = {date: {h: d[h] for h in hours} for date, d in dtd}

TA贡献1848条经验 获得超2个赞
我建议使用,collections.defaultdict因为您的某些计数可能为 0。
这是一个选项:
from collections import defaultdict
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',]
# Nested defaultdict
result = 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}}

TA贡献1827条经验 获得超9个赞
一种快速而肮脏的方法是:
#!/usr/bin/env python3
def 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 ret
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']
print(convert(dtl))
添加回答
举报