Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join items in python list separated by delimiter [duplicate]

I have a list like the following

list_1 = ['>name', 'aaa', 'bbb', '>name_1', 'ccc', '>name_2', 'ddd', 'eee', 'fff']

I was trying to join the items between the items with the '>" sign. So what I want is:

list_1 = ['>name', 'aaabbb', '>name_1', 'ccc', '>name_2', 'dddeeefff']

How can I do that in python?

like image 658
Wilson Mak Avatar asked Dec 26 '22 01:12

Wilson Mak


2 Answers

Use a generator function; that lets you control when items are 'done' to yield:

def join_unescaped(it):
    tojoin = []
    for element in it:
        if element.startswith('>'):
            if tojoin:
                yield ''.join(tojoin)
                tojoin = []
            yield element
        else:
            tojoin.append(element)
    if tojoin:
        yield ''.join(tojoin)

To produce a new list then from your input, pass the generator object produced to the list() function:

result = list(join_unescaped(list_1))

Demo:

>>> list_1 = ['>name', 'aaa', 'bbb', '>name_1', 'ccc', '>name_2', 'ddd', 'eee', 'fff']
>>> def join_unescaped(it):
...     tojoin = []
...     for element in it:
...         if element.startswith('>'):
...             if tojoin:
...                 yield ''.join(tojoin)
...                 tojoin = []
...             yield element
...         else:
...             tojoin.append(element)
...     if tojoin:
...         yield ''.join(tojoin)
... 
>>> list(join_unescaped(list_1))
['>name', 'aaabbb', '>name_1', 'ccc', '>name_2', 'dddeeefff']
like image 159
Martijn Pieters Avatar answered Mar 16 '23 00:03

Martijn Pieters


>>> from itertools import groupby
>>> list_1 = ['>name', 'aaa', 'bbb', '>name_1', 'ccc', '>name_2', 'ddd', 'eee', 'fff']
>>> [''.join(v) for k, v in groupby(list_1, key=lambda s: s.startswith('>'))]
['>name', 'aaabbb', '>name_1', 'ccc', '>name_2', 'dddeeefff']

The only case to watch for here is if you have no items between > signs, which requires a simple fix.

>>> list_1 = ['>name', '>name0', 'aaa', 'bbb', '>name_1', 'ccc', '>name_2', 'ddd', 'eee', 'fff']
>>> [''.join(v) for k,v in groupby(list_1,key=lambda s:s.startswith('>')and s)]
['>name', '>name0', 'aaabbb', '>name_1', 'ccc', '>name_2', 'dddeeefff']

Sub note: just in the extremely unlikely case that you can have duplicate >names like ['>name', '>name', 'aaa'....] just change and s to and object()(which is unique) and that handles every possible case

like image 20
jamylak Avatar answered Mar 15 '23 23:03

jamylak