How to merge correctly to get from this array:
x = [[0,"TEXT1",1,2],[0,"TEXT2",3,4],[0,"TEXT3",6,7],[1,"TEXT4",10,12],[1,"TEXT5",13,14],[1,"TEXT6",16,17],[0,"TEXT7",18,19]]
x[0][0] = User number
x[0][1] = Word
x[0][2] = Time FROM
x[0][3] = Time To
What I need is to get something this:
User 0: TEXT1 TEXT2 TEXT3 From 1 to 7
User 1: TEXT4 TEXT5 TEXT6 From 10 to 17
User 0: TEXT7 From 18 to 19
What is the most efficient way to do it?
- As my idea of creating a lot of subarrays and then combining all together looks like bad coding...
Extra Explanation:
Inside the array we can see these element - [0,"TEXT1",1,2]
- 0 = user ID
- TEXT1 = the word user 0 said
- 1 = start saying it at second 1
- 2 finished saying at second 2
Then after a while we can see - [1,"TEXT4",10,12]
- This is the same as before but here we can see that user (with ID 1) start talking
CodePudding user response:
Here's a solution using itertools.groupby
:
For each group (which is a sequence of a user's elements), we take all the words, the minimum "from" and the maximum "to".
from itertools import groupby
from operator import itemgetter
x = [[0,"TEXT1",1,2],[0,"TEXT2",3,4],[0,"TEXT3",6,7],[1,"TEXT4",10,12],[1,"TEXT5",13,14],[1,"TEXT6",16,17],[0,"TEXT7",18,19]]
result = []
for k,g in groupby(x, key=itemgetter(0)):
g_list = list(g) # you can only iterate g itself once, I'm gonna need 3 times...
result.append([k, [e[1] for e in g_list], (min(e[2] for e in g_list)), (max(e[3] for e in g_list))])
print(result)
It gives the output in a structures way, printing it to exactly match your output should be very easy from there.