1

我需要在我们的服务器日志文件上创建一个时间戳记字典,以小时为键

我不想逐个检查正则表达式并附加(它的python ..有更好的方法)

例如说我有一个清单:

 times = ['02:49:04', '02:50:03', '03:21:23', '03:21:48', '03:24:29', '03:30:29', '03:30:30', '03:44:54', '03:50:11', '03:52:03', '03:52:06', '03:52:30', '03:52:48', '03:54:50', '03:55:21', '03:56:50', '03:57:31', '04:05:10', '04:35:59', '04:39:50', '04:41:47', '04:46:43']

我如何(以pythonic方式)产生类似这样的东西:

其中“0200”将保存 02:00:00 到 02:59:59 之间的值出现的次数

result = { "0200":2, "0300":15, "0400":5 } 
4

4 回答 4

4

就像是:

from collections import Counter
counts = Counter(time[:2]+'00' for time in times)
于 2013-06-17T15:34:28.890 回答
1
from collections import defaultdict
countDict = defaultdict(int)
for t in times:
    countDict[t[:2]+"--"] += 1

print  countDict
于 2013-06-17T15:36:47.487 回答
0

这是另一种方法itertools

import itertools
key = lambda x: x[:2]
result = {}
for hour, group in itertools.groupby(sorted(times, key=key), key=key):
    result[hour + '00'] = len(list(group))
于 2013-06-17T15:43:59.377 回答
0

如果您不想使用计数器。你可以做:

dict = {}
for i in times:
   try:
       dict[i.split(':')[0] + "00"]+=1
   except KeyError:
       dict[i.split(':')[0] + "00"] = 1
于 2013-06-17T15:37:10.560 回答