对于str,输出字符串str中的连续最长的数字串。
a = 'a123879b1234'
maxLen, curLen, maxStr, curStr = 0,0, "", ""
# 维护一个最大字符串
for i,v in enumerate(a):
if v.isdigit():
curLen += 1
curStr += v
if curLen >= maxLen:
maxLen = curLen
maxStr = curStr
else:
curLen = 0
curStr = ""
print maxStr
看到网友还有一种思路,就是使用正则表达式,这种方式比较讨巧
import re
s = raw_input()
a = [[len(i),i] for i in re.findall('\d+', s)]
print max(a)[1]