franchyze923
franchyze923

Reputation: 1200

How to split string everywhere a letter appears?

I have a string containing letters and numbers like this -

12345A6789B12345C

How can I get a list that looks like this

[12345A, 6789B, 12345C]

Upvotes: 1

Views: 87

Answers (3)

Adam Smith
Adam Smith

Reputation: 54273

You could build this with a generator, too. The approach below keeps track of start and end indices of each slice, yielding a generator of strings. You'll have to cast it to list to use it as one, though (splitonalpha(some_string)[-1] will fail, since generators aren't indexable)

def splitonalpha(s):
    start = 0
    for end, ch in enumerate(s, start=1):
        if ch.isalpha:
            yield s[start:end]
            start = end

list(splitonalpha("12345A6789B12345C"))
# ['12345A', '6789B', '12345C']

Upvotes: 0

zwer
zwer

Reputation: 25829

For the sake of completeness, non-regex solution:

data = "12345A6789B12345C"

result = [""]
for char in data:
    result[-1] += char
    if char.isalpha():
        result.append("")
if not result[-1]:
    result.pop()

print(result)
# ['12345A', '6789B', '12345C']

Should be faster for smaller strings, but if you're working with huge data go with regex as once compiled and warmed up, the search separation happens on the 'fast' C side.

Upvotes: 1

Uriel
Uriel

Reputation: 16224

>>> my_string = '12345A6789B12345C'
>>> import re
>>> re.findall('\d*\w', my_string)
['12345A', '6789B', '12345C']

Upvotes: 4

Related Questions