【问题标题】:compress large ranges into shorter ranges in python while maintaining the labels在 python 中将大范围压缩为较短的范围,同时保持标签
【发布时间】:2018-08-17 06:13:01
【问题描述】:

我的代码读取一系列预测并将它们转换为元组(例如 [start,end,label]) 基于多数 %age(>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')

问题:

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

例如,人类会简单地说,

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

谢谢。

【问题讨论】:

    标签: python arrays algorithm python-2.7 tuples


    【解决方案1】:

    你把适合的部分加起来:

    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']]
    

    【讨论】:

    • 非常优雅的解决方案先生!
    • 如果我将预测替换为测试用例,例如:predictions = [0]*60 + [1]* 60,程序将单独返回[0.0, 1.3931972789115648, 'not-music'] 。它没有输出答案的后半部分,应该是[1.39xxx , 2.78639455782, 'music]
    • @DJ_Stuffy_K predictions = [0]*60 + [1]* 61 工作并提供两种输出 - 可能某些条件 rate 是 == 60 会发生什么 - 它在 @ 中不存在987654328@ 也不是 elif .. 所以没有处理。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-27
    • 1970-01-01
    • 2023-03-03
    • 2011-09-28
    • 2012-10-30
    相关资源
    最近更新 更多