在Python中根据时间保持异常计数

我正在尝试将列表转换为特定格式(预期输出),但是遇到了问题。在这里,我试图按照给定的时间和字典顺序对异常进行排序。

我的输出:

21:15-21:30 IllegalAgrumentsException 1,
23:45-00:00 IllegalAgrumentsException 3,
01:00-01:15 IllegalAgrumentsException 0,
01:45-02:00 IllegalAgrumentsException 3,
02:00-02:15 IllegalAgrumentsException 0,
03:00-03:15 IllegalAgrumentsException 0,
21:15-21:30 NullPointerException 1,
21:15-21:30 NullPointerException 1,
22:15-22:30 NullPointerException 1,
23:00-23:15 NullPointerException 0,
23:30-23:45 NullPointerException 2,
23:30-23:45 NullPointerException 2,
00:30-00:45 NullPointerException 2,
01:30-01:45 NullPointerException 2,
02:30-02:45 NullPointerException 2,
22:00-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:00 UserNotFoundException 3,
23:15-23:30 UserNotFoundException 1,
00:00-00:15 UserNotFoundException 0,
00:45-01:00 UserNotFoundException 3,
01:15-01:30 UserNotFoundException 1,
02:15-02: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 #this is wrong since I have to keep count of exception in that time range (please refer exepected output)
        else:
            exception_count[data] = #keep count of exception in that time range 

        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 = "{:02}:{:02}-{:02}:{:02} {} {},".format(hour1, min1, hour2, min2, data, exception_count[data])

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

        print(resultant_arr)

    return HttpResponse(resultant_arr)

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

评论