2014-04-08 65 views

回答

5

您可以使用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'] 
3

您可以使用itertools.groupby,这样

from itertools import groupby 
print ["".join(g) for k,g in groupby('a1b2c', key=str.isdigit) if not k] 
# ['a', 'b', 'c'] 
+2

你应该可以做'groupby('a1b2c',key = str.isdigit)'而不是lambda。 – iCodez

+0

@iCodez谢谢你:)我用你的建议更新了答案。 – thefourtheye