按顺序压缩范围

时间:2012-07-20 04:00:38

标签: python

假设我有这个清单:

a = [('student', '100'), ('student', '101'),
     ('student', '102'), ('student', '103'),
     ('student', '104'), ('student', '105'),
     ('student', '106'), ('student', '120'),
     ('student', '121'), ('student', '122'),
     ('student', '123'), ('student', '124'),
     ('teacher', '21'), ( 'teacher', '22'),
     ('teacher', '23'), ('teacher', '24'),
     ('teacher', '25'), ('teacher', '26'),
     ('teacher', '27'), ('teacher', '51'),
     ('teacher', '52'), ('teacher', '53'),
     ('teacher', '60'), ('Zstudent', '55'),
     ('Zstudent', '56'), ('Zstudent', '57'),
     ('Mstudent', '30'), ('Mstudent', '31')]

我如何输出:

student 100-106 120-124
teacher 22-27 51-53 60
Zstudent 55-57
Mstudent 30-31

1 个答案:

答案 0 :(得分:4)

你可以这样做:

>>> [(i, [int(x[1]) for x in j]) for i,j in 
          itertools.groupby(a, key=operator.itemgetter(0))]
[('student', [100, 101, 102, 103, 104, 105, 106, 120, 121, 122, 123, 124]),
 ('teacher', [21, 22, 23, 24, 25, 26, 27, 51, 52, 53, 60]),
 ('Zstudent', [55, 56, 57]),
 ('Mstudent', [30, 31])]

因此您可以使用结果列表并使用this recipe或@gnibbler提供的nice code创建范围。

如果数据未排序,您可以使用基于dict的解决方案(或defauldict):

>>> d = {}
>>> for k,v in a:
    d.setdefault(k, []).append(int(v))

您只会丢失名称的顺序。