python如何拆分多个字符的字符串?

use*_*211 0 python string split delimiter python-3.x

我想分割一个字符串如下

1234ABC进入123ABC

2B进入2B

10E进入10E

我发现split功能不起作用,因为没有delimiter

hil*_*lem 5

您可以使用itertools.groupby布尔isdigit函数.

from itertools import groupby

test1 = '123ABC'
test2 = '2B'
test3 = '10E'

def custom_split(s):
    return [''.join(gp) for _, gp in groupby(s, lambda char: char.isdigit())]

for t in [test1, test2, test3]:
    print(custom_split(t))

# ['123', 'ABC']
# ['2', 'B']
# ['10', 'E']
Run Code Online (Sandbox Code Playgroud)