Search code examples
pythonlistsplitdelimiter

Split element in list while keeping delimiter


I have a list that I want to split up a bit more, but the output for the data is such that I can't keep it in a good format. Below is an example of what I'm doing

data = ['x', '10[mm]', 'y', '15[mm]']
Data = [data.split('[') for item in data]
Output => Data = ['x', '10', 'mm]', 'y', '15' 'mm]']

And I'm looking to get the output to show

Data = ['x', '10', '[mm]', 'y', '15', '[mm]']

I've seen scripts that keep the delimiter by doing the following and re.split, but I don't know how I could implement this into what I have so far

d = ">"
for line in all_lines:
    s =  [e+d for e in line.split(d) if e]

Solution

  • A regular expression match is the friend you're looking for:

    import re
    
    data = ['x', '10[mm]', 'y', '15[mm]']
    
    pattern = re.compile("^(.*?)(\[.*\])?$")
    
    result = []
    for d in data:
        m = pattern.match(d)
        result.append(m.group(1))
        if m.group(2):
            result.append(m.group(2))
    
    print(result)
    
    
    Result:
    
        ['x', '10', '[mm]', 'y', '15', '[mm]']
    

    There isn't a lot of variance in your test data, so it isn't clear what patterns are possible. I used the most general pattern possible, assuming that there is at most a single portion in square braces, and if there is a square brace expression, it will show up at the end of the input value. With those constraints, the string before the square braces, and the string inside the square braces can be any combination of characters.