Home > Net >  Python Split/Slice a string into a list while keeping delimeter
Python Split/Slice a string into a list while keeping delimeter

Time:11-20

I want to take a string and create a list from it using a delimiter, while keeping delimiter.

If I have "A56-3#AJ4klAP0W" I would like to return a list with A as delimiter.

[A56-3#, AJ4kl, AP0W]

I have tried split and slice but have not been successful. I did do list comprehension to get a list of the index of each delimiter but haven't been able to do much with it [0, 6, 11]

CodePudding user response:

You can use regular expressions and the findall() function.

>>> re.findall('A?[^A] ', 'A56-3#AJ4klAP0W')
['A56-3#', 'AJ4kl', 'AP0W']

This even works when the string doesn't start with your delimiter. E.g.

>>> re.findall('A?[^A] ', '56-3#AJ4klAP0W')
['56-3#', 'AJ4kl', 'AP0W']

Explanation: (Regex101)

A?      : Zero or one "A"
  [^A]  : Followed by one or more "not A"

It's easy to build the regex using an f-string:

def get_substrings(delim, s):
    rex = f"{delim}?[^{delim}] "
    return re.findall(rex, s)

CodePudding user response:

Given:

st="A56-3#AJ4klAP0W"

You can get the index of each delimiter with enumerate:

idx=[i for i,ch in enumerate(st) if ch=='A']

Then slice the string with that index:

>>> [st[x:y] for x,y in zip([0] idx, idx[1:] [len(st)])]
['A56-3#', 'AJ4kl', 'AP0W']
# this is how you use the [0,6,11] list in your question

You can also use a regex split:

>>> re.split(r'(?=A)', st)
['', 'A56-3#', 'AJ4kl', 'AP0W']

Or find the substrings (rather than split) that satisfy that condition:

>>> re.findall(r'A*[^A] ', st)
['A56-3#', 'AJ4kl', 'AP0W']

 

CodePudding user response:

Just add it back in:

>>> x = 'A56-3#AJ4klAP0W'
>>> x.split('A')
['', '56-3#', 'J4kl', 'P0W']
>>> ['A' k for k in x.split('A') if k]
['A56-3#', 'AJ4kl', 'AP0W']
>>>

CodePudding user response:

>>> [f'A{el}' for el in "A56-3#AJ4klAP0W".split('A') if el]
['A56-3#', 'AJ4kl', 'AP0W']
>>>

CodePudding user response:

mystr = "A56-3#AJ4klAP0W"
delim = "A"
lst = mystr.split(delim)
lst = [delim   x for x in lst if x != '']
  • Related