将列表中的数据转换为python中的给定格式

我正在尝试将列表转换为特定格式(预期输出),但出现问题。因此,我尝试根据给定的时间并按字典顺序对异常进行排序。

我的输出:

21:15-21:30 IllegalAgrumentsException 1,
23:45-0:0 IllegalAgrumentsException 3,
1:0-1:15 IllegalAgrumentsException 0,
1:45-2:0 IllegalAgrumentsException 3,
2:0-2:15 IllegalAgrumentsException 0,
3:0-3:15 IllegalAgrumentsException 0,
21:15-21:30 NullPointerException 1,
21:15-21:30 NullPointerException 1,
22:15-22:30 NullPointerException 1,
23:0-23:15 NullPointerException 0,
23:30-23:45 NullPointerException 2,
23:30-23:45 NullPointerException 2,
 0:30-0:45 NullPointerException 2,
1:30-1:45 NullPointerException 2,
2:30-2:45 NullPointerException 2,
22:0-22:15 UserNotFoundException 0,
22:30-22:45 UserNotFoundException 2,
22:30-22:45 UserNotFoundException 2,
22:30-22:45 UserNotFoundException 2,
22:45-23:0 UserNotFoundException 3,
23:15-23:30 UserNotFoundException 1,
0:0-0:15 UserNotFoundException 0,
0:45-1:0 UserNotFoundException 3,
1:15-1:30 UserNotFoundException 1,
2:15-2:30 UserNotFoundException 1,

预期产量:

21:15-21:30 IllegalAgrumentsException 1,
21:15-21:30 NullPointerException 2,
22:00-22:15 UserNotFoundException 1,
22:15-22:30 NullPointerException 1,
22:30-22:45 UserNotFoundException 3,
22:45-23:00 UserNotFoundException 1,
23:00-23:15 NullPointerException 1,
23:15-23:30 UserNotFoundException 1,
23:30-23:45 NullPointerException 2,
23:45-00:00 IllegalAgrumentsException 1,
00:00-00:15 UserNotFoundException 1,
00:30-00:45 NullPointerException 1,
00:45-01:00 UserNotFoundException 1,
01:00-01:15 IllegalAgrumentsException 1,
01:15-01:30 UserNotFoundException 1,
01:30-01:45 NullPointerException 1,
01:45-02:00 IllegalAgrumentsException 1,
02:00-02:15 IllegalAgrumentsException 1,
02:15-02:30 UserNotFoundException 1,
02:30-02:45 NullPointerException 1,
03:00-03:15 IllegalAgrumentsException 1

我通过执行一种特殊的算法获得了输出:

data = 
 {'minute': 27, 'hour': 21, 'second': 12, 'data': 'IllegalAgrumentsException'}
 {'minute': 57, 'hour': 23, 'second': 12, 'data': 'IllegalAgrumentsException'}
 {'minute': 3, 'hour': 1, 'second': 52, 'data': 'IllegalAgrumentsException'}
 {'minute': 53, 'hour': 1, 'second': 52, 'data': 'IllegalAgrumentsException'}
 {'minute': 10, 'hour': 2, 'second': 32, 'data': 'IllegalAgrumentsException'}
 {'minute': 0, 'hour': 3, 'second': 32, 'data': 'IllegalAgrumentsException'}
 {'minute': 27, 'hour': 21, 'second': 12, 'data': 'NullPointerException'}
 {'minute': 27, 'hour': 21, 'second': 12, 'data': 'NullPointerException'}
 {'minute': 17, 'hour': 22, 'second': 12, 'data': 'NullPointerException'}
 {'minute': 7, 'hour': 23, 'second': 12, 'data': 'NullPointerException'}
 {'minute': 40, 'hour': 23, 'second': 32, 'data': 'NullPointerException'}
 {'minute': 40, 'hour': 23, 'second': 32, 'data': 'NullPointerException'}
 {'minute': 30, 'hour': 0, 'second': 32, 'data': 'NullPointerException'}
 {'minute': 37, 'hour': 1, 'second': 12, 'data': 'NullPointerException'}
 {'minute': 43, 'hour': 2, 'second': 52, 'data': 'NullPointerException'}
 {'minute': 0, 'hour': 22, 'second': 32, 'data': 'UserNotFoundException'}
 {'minute': 33, 'hour': 22, 'second': 52, 'data': 'UserNotFoundException'}
 {'minute': 33, 'hour': 22, 'second': 52, 'data': 'UserNotFoundException'}
 {'minute': 33, 'hour': 22, 'second': 52, 'data': 'UserNotFoundException'}
 {'minute': 50, 'hour': 22, 'second': 32, 'data': 'UserNotFoundException'}
 {'minute': 23, 'hour': 23, 'second': 52, 'data': 'UserNotFoundException'}
 {'minute': 13, 'hour': 0, 'second': 52, 'data': 'UserNotFoundException'}
 {'minute': 47, 'hour': 0, 'second': 12, 'data': 'UserNotFoundException'}
 {'minute': 20, 'hour': 1, 'second': 32, 'data': 'UserNotFoundException'}
 {'minute': 27, 'hour': 2, 'second': 12, 'data': 'UserNotFoundException'}


    resultant_arr = []
    exception_count = {}

    for i in data:
        data = i['data']

        hour = i['hour']
        mins = i['minute']
        sec = i['second']

        minInterval = floor((mins + (sec / 100)) / 15)

        if i['data'] in exception_count.keys():
            exception_count[data] = minInterval
        else:
            exception_count[data] = minInterval

        hour1 = hour
        hour2 = hour
        if (minInterval + 1) * 15 >= 60:
            if hour2 == 24:
                hour2 = 0
            else:
                hour2 += 1

        if hour1 >= 24:
            hour1 = hour1 - 24
        if hour2 >= 24:
            hour2 = hour2 - 24

        min1 = minInterval * 15
        if min1 >= 60:
            min1 = min1 - 60

        min2 = (minInterval + 1) * 15
        if min2 >= 60:
            min2 = min2 - 60

        final_tuple = str(hour1) + ":" + str(min1) + '-' + str(hour2) + ":" + str(min2) + " " + str(
            data) + " " + str(exception_count[data]) + ','

        final_tuple.rstrip(',')
        resultant_arr.append(final_tuple)

        print(resultant_arr)

    return HttpResponse(resultant_arr)

一切正常,但我无法获得预期的输出。如何获得预期的输出?

评论