Cybran
Cybran

Reputation: 303

count occurrences of timeframes in a list

I need to create a tally dictionary of time stamps on our server log files with the hours as keys

I dont want to do the long-winded case by case check regular expression and append (its python..there is a better way)

e.g. say I have a list:

 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']

How do I (in a pythonic manner) produce something like so:

where "0200" would hold the number of times a value between 02:00:00 to 02:59:59 occurs

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

Upvotes: 1

Views: 61

Answers (4)

Matthew Plourde
Matthew Plourde

Reputation: 44614

Here's one more way with 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))

Upvotes: 0

Ankur Ankan
Ankur Ankan

Reputation: 3066

If you don't want to use counter. You can do:

dict = {}
for i in times:
   try:
       dict[i.split(':')[0] + "00"]+=1
   except KeyError:
       dict[i.split(':')[0] + "00"] = 1

Upvotes: 0

Joran Beasley
Joran Beasley

Reputation: 113930

from collections import defaultdict
countDict = defaultdict(int)
for t in times:
    countDict[t[:2]+"--"] += 1

print  countDict

Upvotes: 1

mgilson
mgilson

Reputation: 309831

something like:

from collections import Counter
counts = Counter(time[:2]+'00' for time in times)

Upvotes: 4

Related Questions