2

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.

6
  • Are start and end always the first and last element of a row? Commented May 25, 2018 at 21:03
  • No, myList is a list of many tuples. But always start is before end. @user4343502 Commented May 25, 2018 at 21:06
  • Within each tuple, do start and end occur once and only once each? Commented May 25, 2018 at 21:06
  • Also, you removed TBS, is that intentional? Commented May 25, 2018 at 21:07
  • For each tuple there is only one word. I want to say if it was start then store it to a list and then by ignoring other words I should search for end for that particular code and save it in the same list as start. @user4343502 Commented May 25, 2018 at 21:10

4 Answers 4

1

You need to use arr.extend() function

arr = []
arr.append([1,2]) # arr = [[1,2]]
arr = []
arr.extend([1,2])  # arr = [1,2]
Sign up to request clarification or add additional context in comments.

1 Comment

@TemporalWolf: arr.append takes exactly one argument; arr.append(1, 2) is invalid.
1

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

Comments

0

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)

3 Comments

Perfect! extends works here. But I still have multiple rows for each item I don't know why. @Kacper Floriański
I literally just said that... the brr.append(arr) adds arr to brr each loop iteration. You have 6 tuples you iterate over so you will 6 elements in your output.
Perfect! Thanks.
0

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)]     

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.