M
M
maryaturova2021-08-05 19:26:41
Python
maryaturova, 2021-08-05 19:26:41

How to display the length of the list for the last day and the last hour?

On each request to flask, I store the current time in a list:


list.append(round(time.time()))

The list looks like:
[1628179279, 1628179280, 1628179281, 1628179282, 1628179283, 1628179284, 1628179285, 1628179287, 1628179288, 1628179928179290, 1628179291, 162991, 162991, 162991, 162991, 162991, 162991, 16ARD

I need to store the length of the list only for the last day, print () the length for the last day and the last hour.
The length of the list per day is about 500 thousand.
Can you tell me the most resource-intensive approach to output per day and per hour?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MinTnt, 2021-08-05
@maryaturova

Well, it's not that difficult.
First you need to count what time it was exactly a day ago And then start the usual cycle
time.time()-(60*60*24)

time_day_before = time()-(60*60*24)
n = 0
for x in lst:
  if x < time_day_before:
    break
  n += 1
    
    
print(n)

G
gimntut, 2021-08-05
@gimntut

The most economical way is to have 2 counters: for a day and for an hour.
They need to be reset to zero, respectively, at the beginning of the day and at the beginning of the hour.
When adding a new time, increase the counters. That's all. The data will always be up-to-date and there will be no need to look at the list.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question