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 410107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:51:22+00:00 2026-05-12T17:51:22+00:00

I have a series of data points (tuples) in a list with a format

  • 0

I have a series of data points (tuples) in a list with a format like:

points = [(1, 'a'), (2, 'b'), (2, 'a'), (3, 'd'), (4, 'c')]

The first item in each tuple is an integer and they are assured to be sorted. The second value in each tuple is an arbitrary string.

I need them grouped in lists by their first value in a series. So given an interval of 3, the above list would be broken into:

[['a', 'b', 'a', 'd'], ['c']]

I wrote the following function, which works fine on small data sets. However, it is inneficient for large inputs. Any tips on how to rewrite/optimize/mininize this so I can process large data sets?

def split_series(points, interval):
    series = []

    start = points[0][0]
    finish = points[-1][0]

    marker = start
    next = start + interval
    while marker <= finish:
        series.append([point[1] for point in points if marker <= point[0] < next])
        marker = next
        next += interval

    return series
  • 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-05-12T17:51:22+00:00Added an answer on May 12, 2026 at 5:51 pm

    For completeness, here’s a solution with itertools.groupby, but the dictionary solution will probably be faster (not to mention a lot easier to read).

    import itertools
    import operator
    
    def split_series(points, interval):
        start = points[0][0]
    
        return [[v for k, v in grouper] for group, grouper in
                itertools.groupby((((n - start) // interval, val)
                                   for n, val in points), operator.itemgetter(0))]
    

    Note that the above assumes you’ve got at least one item in each group, otherwise it’ll give different results from your script, i.e.:

    >>> split_series([(1, 'a'), (2, 'b'), (6, 'a'), (6, 'd'), (11, 'c')], 3)
    [['a', 'b'], ['a', 'd'], ['c']]
    

    instead of

    [['a', 'b'], ['a', 'd'], [], ['c']]
    

    Here’s a fixed-up dictionary solution. At some point the dictionary lookup time will begin to dominate, but maybe it’s fast enough for you like this.

    from collections import defaultdict
    
    def split_series(points, interval):
        offset = points[0][0]
        maxval = (points[-1][0] - offset) // interval
        vals = defaultdict(list)
        for key, value in points:
            vals[(key - offset) // interval].append(value)
        return [vals[i] for i in xrange(maxval + 1)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have constructed a collection of data series items. Each data series has multiple
I have a series of timestamped data points that I wish to group by
I have arrays of points that contain series data (x & y). Is there
I have plots of 3-axis accelerometer time-series data (t,x,y,z) in separate subplots I'd like
I have hundreds of thousands of points of time series data that I want
I have a variety of time-series data stored on a more-or-less georeferenced grid, e.g.
I have a table of time-series data of which I need to find all
I am trying to have two data series plotted in one graph as boxes
I have a series of tables that contain data I want to full text
I have a tabbed series of grids on a data preview page. I now

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.