- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- from collections import defaultdict
- from itertools import groupby, count
- def parse(filename):
- dic = defaultdict(list)
- with open(filename) as f:
- for line in f:
- lst = line.split()
- dic[lst[0]].append(lst[1])
- for key, value in dic.items():
- lst = sorted(value, key=lambda s: int(s[1:]))
- new_list = []
- for k, g in groupby(lst, lambda s, c=count(): int(s[1:]) - next(c)):
- lg = list(g)
- val = lg[0] if len(lg) == 1 else '{}-{}'.format(lg[0], lg[-1][1:])
- new_list.append(val)
- print(key, ','.join(new_list))
- #该片段来自于http://www.codesnippet.cn/detail/1908201410239.html
来源: http://www.codesnippet.cn/detail/1908201410239.html