这不考虑字符串中间的任何内容,但它基本上表示如果最后一个数字是数字,则它以数字结尾。
In [4]: s = "hello123"
In [5]: s[-1].isdigit()
Out[5]: True
有几个字符串:
In [7]: for s in ['hello12324', 'hello', 'hello1345252525', 'goodbye']:
...: print s, s[-1].isdigit()
...:
hello12324 True
hello False
hello1345252525 True
goodbye False
我完全完全支持正则表达式解决方案,但这里有一种(不漂亮的)方法可以让你得到这个数字。同样,正则表达式在这里要好得多:)
In [43]: from itertools import takewhile
In [44]: s = '12hello123558'
In [45]: r = s[-1::-1]
In [46]: d = [c.isdigit() for c in r]
In [47]: ''.join((i[0] for i in takewhile(lambda (x, y): y, zip(r, d))))[-1::-1]
Out[47]: '123558'