Home > Mobile >  Split element in list while keeping delimiter
Split element in list while keeping delimiter

Time:09-20

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]

CodePudding user response:

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.

CodePudding user response:

Try (maybe the pattern will need adjustment based on your real data):

import re

data = ["x", "10[mm]", "y", "15[mm]"]

pat = re.compile(r"\[mm\]|\d |. ")
out = [p for s in data for p in pat.findall(s)]
print(out)

Prints:

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

CodePudding user response:

Try this:

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

for i in data:
    if re.search(subs, i):
        i = i.replace(f'{subs}', '')
        Data.append(i)
        Data.append(f'{subs}')
        continue
    Data.append(i)

The Output:

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

CodePudding user response:

Yet another variation subject to the same assumptions as the first two.

import re
data = ['x', '10[mm]', 'y', '15[mm]']
r = []
for item in data:
    m = re.match('(.*?)(\[.*?\])', item)
    if m:
        r.append(m[1])
        r.append(m[2])
    else:
        r.append(item)
print(r)

Prints:

['x', '10', '[mm]', 'y', '15', '[mm]']
  • Related