比如:
[3,4,5,6,7,10,11,12,15,16,17,19,20,21,22,23,24,42,43,44,45,46,48]
怎么得到下面的结果呢?
[[3,7],[10,12],[15,17],[19,24],[42,46],[48,48]]
解答代码:
>>> a = [3,4,5,6,7,10,11,12,15,16,17,19,20,21,22,23,24,42,43,44,45,46,48] >>> t1 = [] >>> t2 = [] >>> for x in a: t1.append(x) if x+1 not in a: t2.append([t1[0], t1[-1]]) print(t1[0],t1[-1]) t1 = [] 3 7 10 12 15 17 19 24 42 46 48 48 >>> t2 [[3, 7], [10, 12], [15, 17], [19, 24], [42, 46], [48, 48]] >>>