求一长串数字中出连续出现最多的数,不是出现频率最高的数
比如:277230077000877 返回0而不是7,因为0连续出现最多的一次是3比7多。
322213133 返回2
不能用string、list做,只能用loop
# -*- coding: UTF-8 -*-
import re
s = "277230077000877"
matches = re.finditer(r'(\d)\1{1,}',s)
results = list([match.group(0) for match in matches])
results.sort(key=lambda x: -len(x))
print(results[0][0])
0
不同string list用循环:
import re
s = "277230077000877"
matches = re.finditer(r'(\d)\1{1,}',s)
ma = ""
for match in matches:
if len(ma) < len(match.group(0)): ma = match.group(0)
print(ma[0])