I have a list like this:
mylist = [(20, 'Start', '2008-10-10', 'TBS'),...,(20, 'End', '2008-11-09', 'NG'),
(21, 'Start', '2008-12-10', 'TBS'),...,(21, 'End', '2008-12-15', 'G'),
(22, 'Start', '2009-01-10', 'TBS'),...,(22, 'End', '2009-12-10', 'B'),..]
I put '...' in the example above to say there are other items for each id like 20, 21 and 22 in the list but I don't want them. The only items that I want are the items that include 'Start' or 'End'.(Other items have different words than these two words.)
I want to create a nested list like this:
[[20, 'Start', '2008-10-10', 'End', '2008-11-09', 'NG'] ,
[21, 'Start', '2008-12-10', 'End', '2008-12-15', 'G'],
[22, 'Start', '2009-01-10', 'End', '2009-12-10', 'B']]
Here is my code:
code = 0
brr = []
for row in myList:
if row[1] == "Start":
arr = []
code = row[0]
arr.append([row[0], row[1], row[2]])
continue
if row[0] == code and row[1] == "End":
arr.append([row[1], row[2], row[3]])
brr.append(arr)
for k in brr:
print(k)
But the problem is that it creates something like this:
[[[20, 'Start', '2008-10-10', 'End'], ['2008-11-09', 'NG']] ,
[[20, 'Start', '2008-10-10', 'End'], ['2008-11-09', 'NG']] ,
[[20, 'Start', '2008-10-10', 'End'], ['2008-11-09', 'NG']] ,
[[21, 'Start', '2008-12-10', 'End'], ['2008-12-15', 'G']],
[[21, 'Start', '2008-12-10', 'End'], ['2008-12-15', 'G']],
[[22, 'Start', '2009-01-10', 'End'], ['2009-12-10', 'B']]]
And for each items I have multiple rows in the list. I don't know why? Sorry if my question is too long.
TBS, is that intentional?