Reputation: 405
I have the following Python list of tuples:
a = [(1,2,'MARCO'),(3,4,'MARCO'),(5,6,'MARCO'),(7,8,'MARCO'),(9,11,'CARLO'),(12,13,'CARLO'),(14,15,'CARLO'),(16,17,'MARCO'),(18,19,'MARCO'),(20,21,'MARCO'),(22,23,'MARCO')]
In each tuple the first element is the starting time, the second element the end time and the third the speaker active in that segment. What is an efficient way to group consecutive intervals with the same speaker and having the total initial time and the total final time?
More specifically, the output I'd like to obtain is something like the following:
b = [(1,8,'MARCO'),(9,15,'CARLO'),(16,23,'MARCO')]
Upvotes: 2
Views: 530
Reputation: 336258
How about a little handmade fun with the itertools
module? This assumes that your list is ordered by rising starting "times".
from itertools import groupby
def speaker_times(a):
result = []
for item in groupby(a, lambda x:x[2]): # Group by speaker name
values = list(item[1]) # Convert iterator to list
result.append((min(values)[0], max(values)[1], item[0]))
return result
Result:
In [12]: speaker_times(a)
Out[12]: [(1, 8, 'MARCO'), (9, 15, 'CARLO'), (16, 23, 'MARCO')]
Upvotes: 7