如何拆分分隔符为Python数据类型的字符串?
例如:
str_to_split = 'a1b2c'
str_to_split.split(int)
# ['a','b','c']
您可以使用re.split():
>>> import re
>>> thingToSplit = 'a1b2c'
>>> re.split('d+', thingToSplit)
['a', 'b', 'c']
或者,应用isdigit()检查:
>>> [item for item in thingToSplit if not item.isdigit()]
['a', 'b', 'c']
您可以使用itertools.groupby
,就像这个
from itertools import groupby
print ["".join(g) for k,g in groupby('a1b2c', key=str.isdigit) if not k]
# ['a', 'b', 'c']