Python Forum
Grouping a list of various time into intervals
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Grouping a list of various time into intervals
#1
Hi, I am looking to create a new function that will take in a list of different times and also another input which will split it into x second intervals.

The function needs to split the times and group them with a count.

So for example I pass to the function the list and the value 10, this means to split each time into 10 second intervals. I then want to count for each value within the list the times that are within each group. So any times within the first 10 seconds this is returned as a count. I count the times within the list for the second 10 seconds and so on.

I am thinking i could do this by first getting the min and max times of the values within the list. Use a WHILE time <= max(time) get the min(time) and keep adding x=10 (if we set to 10 second intervals). This will give me a list of each time intervals that will be required. From this point I would need to loop over my list of times and compare it to which group in the time interval list(not quite sure exactly how i would do this)

I am thinking there is most likely a module that may already do this as it must be common for graphs and scales? Can someone give me a pointer in how I should get started.

Thanks in advance
Reply
#2
what's the start for first 10 seconds interval? the min time in the list?
you can find the time difference between start and each time in the list and divide that by interval length. so you can find in which interval is each time.
from datetime import datetime
from itertools import groupby
duration = 10
times = ['10:00:15', '10:00:17', '10:00:29', '10:00:30', '10:00:50']
times = sorted([datetime.strptime(t, '%H:%M:%S') for t in times])
start = times[0]
for interval, cnt in groupby(times, lambda t: int((t - start).total_seconds() // duration) + 1):
    times = [t.strftime('%H:%M:%S') for t in cnt]
    print (f'interval:{interval}, count:{len(times)}, times:{times}')
Output:
interval:1, count:2, times:['10:00:15', '10:00:17'] interval:2, count:2, times:['10:00:29', '10:00:30'] interval:4, count:1, times:['10:00:50']
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Grouping Data based on 30% bracket purnima1 4 1,196 Mar-10-2023, 07:38 PM
Last Post: deanhystad
  Grouping and sum of a list of objects Otbredbaron 1 3,217 Oct-23-2021, 01:42 PM
Last Post: Gribouillis
  Time.sleep: stop appending item to the list if time is early quest 0 1,880 Apr-13-2021, 11:44 AM
Last Post: quest
  time intervals project_science 7 2,966 Feb-06-2021, 11:49 PM
Last Post: project_science
  Plotting “simple” diagram with gridlines at specified intervals schniefen 1 2,424 Dec-03-2020, 05:54 PM
Last Post: schniefen
  concatenating 2 items at a time in a python list K11 3 2,341 Oct-21-2020, 09:34 AM
Last Post: buran
  Grouping and summing of dataset jef 0 1,645 Oct-04-2020, 11:03 PM
Last Post: jef
  Setting Tick Intervals In Sub Plots JoeDainton123 1 1,805 Oct-03-2020, 11:52 PM
Last Post: scidam
  Appending list Trouble Big Time Milfredo 7 3,126 Oct-01-2020, 02:59 AM
Last Post: Milfredo
  Grouping algorithm riccardoob 7 3,033 May-19-2020, 01:22 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020