Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8023485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:34:34+00:00 2026-06-04T22:34:34+00:00

Say that you have a sorted list of n timestamps (Python datetime objects). How

  • 0

Say that you have a sorted list of n timestamps (Python datetime objects). How would you yield a list of tuples of the form (t, count), where t is a datetime object and count is the number of elements in the list at most x minutes from t?

For example, given the dates (strings, for brevity; in reality datetime objects):

timestamps = ["13:00", "13:01", "13:03", "13:04", "13:05", "13:06", "13:09"]

if x is two minutes, then yield

[("13:00", 2), ("13:03":3), ("13:06":1), ("13:09", 1)]]

What I’m trying to do is make a coarser list of hits on a resource, and the only data I have is the access time of every hit (the granularization is to the millisecond, and I’d like it granular to the minute, or ten minutes)

I would post my attempts, but I’m ashamed…

Edit: This is what I have so far… testing to see if it works…

def group_timestamps(timestamps, chunksize=10):
    """Groups a list of timestamps in chunks of ``chunksize`` minutes"""
    cs = timedelta(minutes=chunksize)

    if not timestamps:
        return []

    t0 = timestamps[0]
    count = 1
    chunks = []

    for ts in timestamps:
        if (ts - t0) <= cs:
            count += 1
        else:
            chunks.append((t0, count))
            t0 = ts
            count = 1
    return chunks
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T22:34:37+00:00Added an answer on June 4, 2026 at 10:34 pm

    This should work:

    current = timestamps[0]
    count = 0
    res = []
    for t in timestamps:
        if (t - current) <=  timedelta(minutes= 2): 
             count = count + 1
        else:
             res.append((current,count))
             current = t
             count = 1
    res.append(current,count) #add last tuple
    

    Following your example:

    timestamps = [datetime(hours=13,minutes=00), datetime(hours=13,minutes=01), datetime(hours=13,minutes=03), datetime(hours=13,minutes=04), datetime(hours=13,minutes=05), datetime(hours=13,minutes=06), datetime(hours=13,minutes=09)]
    
    res = [(datetime(hours=13,minutes=00),2),(datetime(hours=13,minutes=03),3),(datetime(hours=13,minutes=06),1),(datetime(hours=13,minutes=09),1)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have a class that contains a list of sorted numbers. The
Say I have a List [1,2,4,5], I would like to have a predicate that
Lets say that you have a business object whose current state implies that there
Lets say that I have only DHT (distributed hash table) implemented (in Python), and
I have a list of objects say, List. The Entity class has an equals
Let's say I have a list of objects. (All together now: I have a
Lets say I have a routine that scans an entire list of n items
I have an array with a list of objects sorted alphabetically ignoring the letters
I have a list of integers that contains a number of values (say, 200).
Say that I have an article with multiple pages. Each page has a short

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.