pythonlistsplit

Create nested lists based on split of characters


I have a list made by strings, correctly cleaned (split(',') can be safely used), and correctly sorted depending on numbers. As a small example:

l = ['C1', 'C1,C2', 'C2,C3', 'C3,C4', 'C4', 'C5', 'C5,C6', 'C6,C7', 'C7,C8', 'C8', 'C10', 'C10,C11', 'C11,C12', 'C12,C13', 'C13']

What I'm trying to achieve is to create as many sublists that start and end with single strings, that is:

[
    ['C1', 'C1,C2', 'C2,C3', 'C3,C4', 'C4'],
    ['C5', 'C5,C6', 'C6,C7', 'C7,C8', 'C8'],
    ['C10', 'C10,C11', 'C11,C12', 'C12,C13', 'C13']
]

I thought to add some logic like the following code, but I'm not sure if I'm on the correct way:

tl = []

for i in l:
    
    # just get the variable
    val = i
    
    tl.append(val)
    
    # split by ,
    val_split = len(i.split(','))  
    
    # check if the value is the first element of the list (C1)
    if val == l[0]:
        print(1, val)
    # check if the split of the character is longer than 2 (C1,C2)
    elif val_split > 1:
        print(2, val)
    # check is the split of the character siis equalt to 1 (C4)
    elif val_split == 1:
        # here the code should compare if the character is equal to the last value of the nested list. If yes go with teh next value (C5)
        if val != tl[-1]:
            print(3, val)
        else:
            print(4, val)

Solution

  • If the input list is guaranteed to start and end with a single string and if there will always be at least one adjacent pair of single strings then:

    lst = ['C1', 'C1,C2', 'C2,C3', 'C3,C4', 'C4', 'C5', 'C5,C6', 'C6,C7', 'C7,C8', 'C8', 'C10', 'C10,C11', 'C11,C12', 'C12,C13', 'C13']
    result = [[]]
    for e in lst:
        result[-1].append(e)
        if not "," in e:
            if len(result[-1]) > 1:
                result.append([])
    result.pop()
    print(result)
    

    Output:

    [['C1', 'C1,C2', 'C2,C3', 'C3,C4', 'C4'], ['C5', 'C5,C6', 'C6,C7', 'C7,C8', 'C8'], ['C10', 'C10,C11', 'C11,C12', 'C12,C13', 'C13']]