Time points are classified into different intervals ( Period of time ), Count the number of occurrences in each time period ,Python
Time division is divided into different time intervals ,Python_zhangphil The blog of -CSDN Blog use Python Implement a function , Divide a time into different time ranges , such as 2305, Transfer this time to (23,24) Within the interval . And such as 056, Transfer in (0,1) Within the scope of .https://blog.csdn.net/zhangphil/article/details/125909666
Divide different time into different time periods , Count the number of time points in each time period , such as 21:12 Transfer in to (21,22) This time period , At the same time (21,22) This time period counts 1, If there is another time point 21:02, that 21:02 Still transferred to (21,22) Period of time , And for (21,22) increase 1 Times count , Turn into 2.
import datetime
import random
# Number of times to generate random tests
from pprint import pprint
SAMPLE_COUNT = 10
SECTION = 'section'
SUM = 'sum'
def my_time():
times = []
for i in range(24):
times.append({SECTION: (i, i + 1), SUM: 0})
cnt = 0
while True:
h = random.randint(0, 23)
m = random.randint(0, 59)
t = datetime.time(hour=h, minute=m)
for tx in times:
if tx[SECTION][0] <= t.hour < tx[SECTION][1]:
tx[SUM] = tx[SUM] + 1
pprint(f'{t.strftime("%H:%M")} @ {tx[SECTION]}')
break
cnt = cnt + 1
if cnt > SAMPLE_COUNT:
break
return times
if __name__ == '__main__':
timex = my_time()
pprint(timex)
Output :
'00:34 @ (0, 1)'
'21:58 @ (21, 22)'
'13:47 @ (13, 14)'
'08:01 @ (8, 9)'
'16:45 @ (16, 17)'
'08:19 @ (8, 9)'
'21:02 @ (21, 22)'
'22:06 @ (22, 23)'
'18:20 @ (18, 19)'
'08:36 @ (8, 9)'
'01:45 @ (1, 2)'
[{'section': (0, 1), 'sum': 1},
{'section': (1, 2), 'sum': 1},
{'section': (2, 3), 'sum': 0},
{'section': (3, 4), 'sum': 0},
{'section': (4, 5), 'sum': 0},
{'section': (5, 6), 'sum': 0},
{'section': (6, 7), 'sum': 0},
{'section': (7, 8), 'sum': 0},
{'section': (8, 9), 'sum': 3},
{'section': (9, 10), 'sum': 0},
{'section': (10, 11), 'sum': 0},
{'section': (11, 12), 'sum': 0},
{'section': (12, 13), 'sum': 0},
{'section': (13, 14), 'sum': 1},
{'section': (14, 15), 'sum': 0},
{'section': (15, 16), 'sum': 0},
{'section': (16, 17), 'sum': 1},
{'section': (17, 18), 'sum': 0},
{'section': (18, 19), 'sum': 1},
{'section': (19, 20), 'sum': 0},
{'section': (20, 21), 'sum': 0},
{'section': (21, 22), 'sum': 2},
{'section': (22, 23), 'sum': 1},
{'section': (23, 24), 'sum': 0}]
Project IntroductionAccess con
Catalog Time :2022.6.23