You can do as follows. Start with matching numbers with 4 or more digits, (the regex "\d{4,}" matches 4 or more digits) and extract all numbers. Then group consecutive numbers together using more_itertools.consecutive_groups, and create the result list, and then print it
import re
import os
from more_itertools import consecutive_groups
files = ["default.xml", "df_ak01.1001.jpg", "df_ak01.1002.jpg", "df_ak01.1003.jpg", "df_ak01.1005.jpg", "df_ak01.1006.jpg"]
#Pattern to match numbers with 4 or more digits
pattern = re.compile("\d{4,}")
#Extract all numbers
a = [int(pattern.search(x).group(0)) for x in files if pattern.search(x)]
#[1001, 1002, 1003, 1005, 1006]
#Group consecutive numbers together
cons_groups = [list(group) for group in consecutive_groups(a)]
#[[1001, 1002, 1003], [1005, 1006]]
#Create result list
result = [ [len(x), '{}-{}'.format(x[0], x[-1])] for x in cons_groups]
#[[3, '1001-1003'], [2, '1005-1006']]
#Print the result list
for item in result:
print('{} df_ak01.%04d.jpg {}'.format(item[0], item[1]))
输出将是
3 df_ak01.%04d.jpg 1001-1003
2 df_ak01.%04d.jpg 1005-1006