user9831231
user9831231

Reputation: 37

Create a list from a list of tuples

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.

Upvotes: 2

Views: 354

Answers (4)

Pyd
Pyd

Reputation: 6159

please try this,

startlist=[]
endlist=[]
for item in mylist:
    if 'Start' in list(item):
        startlist.append(list(item))
    elif 'End' in list(item):
        endlist.append(list(item))
outlist=[i+j for i,j in zip(startlist,endlist)]     

Upvotes: 0

Taylor D. Edmiston
Taylor D. Edmiston

Reputation: 13016

You can achieve this pretty simply with itertools.groupby as well:

import itertools
from pprint import pprint

mylist = [
    (20, 'Start', '2008-10-10', 'TBS'),
    (20, 'Foo', '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'),
]

rows = (x for x in mylist if x[1] in ('Start', 'End'))
grouped = itertools.groupby(rows, key=lambda x: x[0])
output = [[k, *next(grp)[1:3], *next(grp)[1:4]] for k, grp in grouped]
pprint(output)

Output:

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

Upvotes: 1

Kacperito
Kacperito

Reputation: 1347

Your brr.append(arr) is always adding an array for each row, that's why there are 6 elements in the ouput. Change brr.append(arr) to:

if arr not in brr:
    brr.append(arr)

As for the format, arr.append([row[0], row[1], row[2]]) adds a list of 3 elements, instead of 3 separate elements. Use extend instead.

Your final code should look like this:

code = 0
brr = []
for row in mylist:

    if row[1] == "Start":
        arr = []
        code = row[0]
        arr.extend([row[0], row[1], row[2]])
        # continue not needed here

    if row[0] == code and row[1] == "End":
        arr.extend([row[1], row[2], row[3]])

    if arr not in brr:
        brr.append(arr)

for k in brr:
    print(k)

Upvotes: 0

Kind Scorpion
Kind Scorpion

Reputation: 11

You need to use arr.extend() function

arr = []
arr.append([1,2]) # arr = [[1,2]]
arr = []
arr.extend([1,2])  # arr = [1,2]

Upvotes: 1

Related Questions