在保持标签的同时将python中的大范围压缩到更短的范围内

时间:2018-03-08 19:13:25

标签: python arrays algorithm python-2.7 tuples

我的代码读取一系列预测并将它们转换为元组(例如  [start,end,label])基于多数%年龄(> x),如下所示:

Sample_predictions = [0,0,0,0,0,0,1,1,1,1,1,0,1,0,1,1,1,1,1,0,0,0,0]  

实际预测值为here

当前输出的前几行看起来像:

(0.0, 1.3931972789115648, 'not-music')
(1.3931972789115648, 2.7863945578231295, 'not-music')
(2.7863945578231295, 4.179591836734694, 'not-music')
(4.179591836734694, 5.572789115646259, 'not-music')
(5.572789115646259, 6.965986394557823, 'not-music')
(6.965986394557823, 8.359183673469389, 'not-music')
(8.359183673469389, 9.752380952380953, 'not-music')
(9.752380952380953, 11.145578231292518, 'not-music')
(11.145578231292518, 12.538775510204083, 'not-music')
(12.538775510204083, 13.931972789115646, 'not-music')
(13.931972789115646, 15.32517006802721, 'not-music')
(15.32517006802721, 16.718367346938777, 'not-music')
(16.718367346938777, 18.11156462585034, 'not-music')
(18.11156462585034, 19.504761904761907, 'not-music')
(19.504761904761907, 20.89795918367347, 'not-music')
(20.89795918367347, 22.291156462585036, 'not-music')
(22.291156462585036, 23.6843537414966, 'not-music')
(23.6843537414966, 25.077551020408166, 'not-music')
(25.077551020408166, 26.470748299319727, 'not-music')
(26.470748299319727, 27.86394557823129, 'not-music')
(27.86394557823129, 29.257142857142856, 'not-music')
(29.257142857142856, 30.65034013605442, 'not-music')
(30.65034013605442, 32.04353741496599, 'music')
(32.04353741496599, 33.436734693877554, 'music')
(33.436734693877554, 34.82993197278912, 'music')

ISSUE:

如何整合此输出以使其尽可能真实?

例如,人类会简单地说,

 0, 30.65, 'not-music'  
 30.65, 34.82, 'music

谢谢。

1 个答案:

答案 0 :(得分:2)

您可以添加适合的部分:

tups = [(0.0, 1.3931972789115648, 'not-music')                  ,
        (1.3931972789115648, 2.7863945578231295, 'not-music')   ,
        (2.7863945578231295, 4.179591836734694, 'not-music')    ,
        (4.179591836734694, 5.572789115646259, 'not-music')     ,
        (5.572789115646259, 6.965986394557823, 'not-music')     ,
        (6.965986394557823, 8.359183673469389, 'not-music')     ,
        (8.359183673469389, 9.752380952380953, 'not-music')     ,
        (9.752380952380953, 11.145578231292518, 'not-music')    ,
        (11.145578231292518, 12.538775510204083, 'not-music')   ,
        (12.538775510204083, 13.931972789115646, 'not-music')   ,
        (13.931972789115646, 15.32517006802721, 'not-music')    ,
        (15.32517006802721, 16.718367346938777, 'not-music')    ,
        (16.718367346938777, 18.11156462585034, 'not-music')    ,
        (18.11156462585034, 19.504761904761907, 'not-music')    ,
        (19.504761904761907, 20.89795918367347, 'not-music')    ,
        (20.89795918367347, 22.291156462585036, 'not-music')    ,
        (22.291156462585036, 23.6843537414966, 'not-music')     ,
        (23.6843537414966, 25.077551020408166, 'not-music')     ,
        (25.077551020408166, 26.470748299319727, 'not-music')   ,
        (26.470748299319727, 27.86394557823129, 'not-music')    ,
        (27.86394557823129, 29.257142857142856, 'not-music')    ,
        (29.257142857142856, 30.65034013605442, 'not-music')    ,
        (30.65034013605442, 32.04353741496599, 'music')         ,
        (32.04353741496599, 33.436734693877554, 'music')        ,
        (33.436734693877554, 34.82993197278912, 'music')]

curr = None     # current listed tuple that we accumulate
consol = []     # consolidated list
for t in tups:
    if curr==None:
        curr = list(t)
        continue

    if t[0] == curr[1] and t[2] == curr[2]:        # same, add up
        curr[1] = t[1]
    else:
        consol.append(curr)       # different, remember, use next
        curr = list(t)
        t = []

consol.append(curr)

print(consol)

输出:

[[0.0, 30.65034013605442, 'not-music'], 
 [30.65034013605442, 34.82993197278912, 'music']]