Home>

I want to decompose the list.

[['hello', 'my', 'job', ['aaa', 'bbb']], ['we', 'i', ['test', 'item']] , ['she', 'is', 'my', 'bos', ['test', 'item']]]


There is a list. This list is

[['hello', 'my', 'job'], ['we', 'i'], ['she', 'is', 'my', 'bos']]


And the list

[['aaa', 'bbb'], ['test', 'item'], ['test', 'item']]


I want to divide the list.

a = []
b = []
for item in data:
    num = len (item)-1
    for i in range (len (itemr)):
        if i! = num:
             a.append (item [i])
        else:
             b.append (item [i])


I wrote the code, but the list a is duplicated in vain.
How can I fix it?

  • Answer # 1

    I don't understand the expression "duplicate uselessly", but
    For the time being, if you put a temporary list as follows, you will get the desired result.

    data = [['hello', 'my', 'job', ['aaa', 'bbb']], ['we', 'i', [ 'test', 'item']], ['she', 'is', 'my', 'bos', ['test', 'item']]]
    a = []
    b = []
    for item in data:
        num = len (item)-1
        tmp_a = []
        for i in range (len (item)):
            if i! = num:
                 tmp_a.append (item [i])
            else:
                 b.append (item [i])
        a.append (tmp_a)
    print (a)
    print (b)

    ResultsWandbox

    [['hello', 'my', 'job'], ['we', 'i'], ['she', 'is', 'my', 'bos']]
    [['aaa', 'bbb'], ['test', 'item'], ['test', 'item']]
    I wrote it Although there is a waste of referring to the

    element twice, this seems easier to understand.

    a = [row [:-1] for row in data]
    b = [row [-1] for row in data]

    Wandbox

    If you really want to complete it in one round, it looks like this.

    a = []
    b = []
    for * elems, last in data:
        a.append (elems)
        b.append (last)

    Wandbox

  • Answer # 2

    I thought about sorting the list elements in the second row into a list and something not.
    I tried writing in two and a half ways.

    sep.py

    from itertools import chain
    data = [
        ['hello', 'my', 'job', ['aaa', 'bbb']],
        ['we', 'i', ['test', 'item']],
        ['she', 'is', 'my', 'bos', ['test', 'item']]
    ]
    data_elem = [[e for e in v if not isinstance (e, list)] for v in data]
    data_list = [[e for e in v if isinstance (e, list)] for v in data]
    print (data_elem)
    print (list (chain.from_iterable (data_list)))
    print ()
    data_0 = list (chain.from_iterable (data))
    print ('data_0 =', data_0)
    data_list = [x for x in data_0 if isinstance (x, list)]
    print (data_list)
    print ()
    data_elem = []
    data_list = []
    for v in data:
        v0 = []
        for e in v:
            if isinstance (e, list):
                data_list.append (e)
            else:
                v0.append (e)
        data_elem.append (v0)
    print (data_elem)
    print (data_list)

    Execution result

  • Answer # 3

    Not recommended, but Just that you can write.

    data = [['hello', 'my', 'job', ['aaa', 'bbb']], ['we', 'i', [ 'test', 'item']], ['she', 'is', 'my', 'bos', ['test', 'item']]]
    a = []
    b = []
    [(a.append (d [:-1]), b.append (d [-1])) for d in data]
    print (a)
    print (b)

    By the way, this is a technique when you want to execute multiple lines in a lambda expression.
    There is a feeling of using def, but for the abandoned code.

    https://stackoverflow.com/questions/862412/is-it-possible-to-have-multiple-statements-in-a-python-lambda-expression