2

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]

azro
  • 53,056
  • 7
  • 34
  • 70
Cara
  • 33
  • 3
  • Does this answer your question? [In Python, how do I split a string and keep the separators?](https://stackoverflow.com/questions/2136556/in-python-how-do-i-split-a-string-and-keep-the-separators) – G. Anderson Nov 19 '21 at 18:40
  • 1
    Is "A" really a *delimiter*, or do you want substrings starting with "A"? – chepner Nov 19 '21 at 18:49
  • I believe you are correct, this should be substring, not delimiter. – Cara Nov 22 '21 at 01:42

4 Answers4

2

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)
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
2

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']

 
dawg
  • 98,345
  • 23
  • 131
  • 206
  • This is a great solution as well and works if the string doesn't start with delimiter. – Cara Nov 19 '21 at 19:21
0

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']
>>>
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • This only works if the delimiter is the first character. For `x = '56-3#AJ4klAP0W'`, there's an extra `A` at the start of the first string – Pranav Hosangadi Nov 19 '21 at 18:42
  • The OP will have to clarify the requirements. I'm guessing from the question that the string WILL always start with an A. – Tim Roberts Nov 19 '21 at 18:50
  • The string can start with any item. This does work when string starts with Delimiter. I can handle the problem when string doesn't start with delimiter. Thank you for all the wonderful help – Cara Nov 19 '21 at 19:01
0
>>> [f'A{el}' for el in "A56-3#AJ4klAP0W".split('A') if el]
['A56-3#', 'AJ4kl', 'AP0W']
>>>
mrvol
  • 2,575
  • 18
  • 21