How to group and count items (read from a file) by time?

Viewed 85

I'm trying to take an input from a file where a line contains 2 fields: (TIME, STATUS)

13:23, HELLO
15:34, BYE
16:34, TESTING
13:12, BYE

Using Python, what I've done is gotten the input file into the following format in a list:

[('13:00', 'HELLO'), ('16:00', 'TESTING'), ('18:00', 'BYE'), ('15:00', 'BYE'), ('18:00', 'HELLO'), ('13:00', 'BYE'), ('13:00', 'HELLO')]

So then I can use Counter from collections to count how many times the STATUS appears giving me the below:

Counter({('13:00', 'HELLO'): 2, ('16:00', 'TESTING'): 1, ('18:00', 'BYE'): 1, ('15:00', 'BYE'): 1, ('18:00', 'HELLO'): 1, ('13:00', 'BYE'): 1})

What I want to do is count how many times the STATUS appears and then group it based on time so that the final output is: (TIME, HELLO COUNT, BYE COUNT, TESTING COUNT)

(13:00, 2, 1, 0)
(16:00, 0, 0, 1)
(18:00, 1, 1, 0)
(15:00, 0, 1, 0)

However, I can't figure out how to get it into that format.

I tried to use a list comprehension but didn't understand as to how I can pass the counter of each STATUS into the list all at once.

Any advice as to how to go about it?

2 Answers

You can use a collections.defaultdict of collections.Counter to count your items.

It first groups your Counter object into something like {'13:00': Counter({'HELLO': 2, 'BYE': 1}), '16:00': Counter({'TESTING': 1}), then just unpacks the result into (TIME, HELLO COUNT, BYE COUNT, TESTING COUNT) tuples.

from collections import Counter
from collections import defaultdict

d = Counter({('13:00', 'HELLO'): 2, ('16:00', 'TESTING'): 1, ('18:00', 'BYE'): 1, ('15:00', 'BYE'): 1, ('18:00', 'HELLO'): 1, ('13:00', 'BYE'): 1})

count_keys = ['HELLO', 'BYE', 'TESTING']

# count and group 
counts = defaultdict(Counter)
for (time, count), testing in d.items():
    counts[time][count] += testing

# output and unpack items from count dictionary
print([(k1, *(v1[k2] for k2 in count_keys)) for k1, v1 in counts.items()])

Output:

[('13:00', 2, 1, 0), ('16:00', 0, 0, 1), ('18:00', 1, 1, 0), ('15:00', 0, 1, 0)]

Below code can be used.

items = [('13:00', 'HELLO'), ('16:00', 'TESTING'), ('18:00', 'BYE'), ('15:00', 'BYE'), ('18:00', 'HELLO'), ('13:00', 'BYE'), ('13:00', 'HELLO')]
data = {}
indexs = {"HELLO": 0, "BYE": 1, "TESTING": 2}
for item in items:
     if item[0] not in data:
         data[item[0]] = [0,0,0]
     data[item[0]][indexs[item[1]]] += 1
print([tuple([k]+v) for k, v in data.items()])
Related