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

  • Home
  • SEARCH
  • 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 8103369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:31:45+00:00 2026-06-05T23:31:45+00:00

I have a 1D numpy array, and some offset/length values. I would like to

  • 0

I have a 1D numpy array, and some offset/length values. I would like to extract from this array all entries which fall within offset, offset+length, which are then used to build up a new ‘reduced’ array from the original one, that only consists of those values picked by the offset/length pairs.

For a single offset/length pair this is trivial with standard array slicing [offset:offset+length]. But how can I do this efficiently (i.e. without any loops) for many offset/length values?

Thanks,
Mark

  • 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-05T23:31:47+00:00Added an answer on June 5, 2026 at 11:31 pm

    There is the naive method; just doing the slices:

    >>> import numpy as np
    >>> a = np.arange(100)
    >>> 
    >>> offset_length = [(3,10),(50,3),(60,20),(95,1)]
    >>>
    >>> np.concatenate([a[offset:offset+length] for offset,length in offset_length])
    array([ 3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 50, 51, 52, 60, 61, 62, 63,
           64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 95])
    

    The following might be faster, but you would have to test/benchmark.

    It works by constructing a list of the desired indices, which is valid method of indexing a numpy array.

    >>> indices = [offset + i for offset,length in offset_length for i in xrange(length)]
    >>> a[indices]
    array([ 3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 50, 51, 52, 60, 61, 62, 63,
           64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 95])
    

    It’s not clear if this would actually be faster than the naive method but it might be if you have a lot of very short intervals. But I don’t know.

    (This last method is basically the same as @fraxel’s solution, just using a different method of making the index list.)


    Performance testing

    I’ve tested a few different cases: a few short intervals, a few long intervals, lots of short intervals. I used the following script:

    import timeit
    
    setup = 'import numpy as np; a = np.arange(1000); offset_length = %s'
    
    for title, ol in [('few short', '[(3,10),(50,3),(60,10),(95,1)]'),
                      ('few long', '[(3,100),(200,200),(600,300)]'),
                      ('many short', '[(2*x,1) for x in range(400)]')]:
      print '**',title,'**'
      print 'dbaupp 1st:', timeit.timeit('np.concatenate([a[offset:offset+length] for offset,length in offset_length])', setup % ol, number=10000)
      print 'dbaupp 2nd:', timeit.timeit('a[[offset + i for offset,length in offset_length for i in xrange(length)]]', setup % ol, number=10000)
      print '    fraxel:', timeit.timeit('a[np.concatenate([np.arange(offset,offset+length) for offset,length in offset_length])]', setup % ol, number=10000)
    

    This outputs:

    ** few short **
    dbaupp 1st: 0.0474979877472
    dbaupp 2nd: 0.190793991089
        fraxel: 0.128381967545
    ** few long **
    dbaupp 1st: 0.0416231155396
    dbaupp 2nd: 1.58000087738
        fraxel: 0.228138923645
    ** many short **
    dbaupp 1st: 3.97210478783
    dbaupp 2nd: 2.73584890366
        fraxel: 7.34302687645
    

    This suggests that my first method is the fastest when you have a few intervals (and it is significantly faster), and my second is the fastest when you have lots of intervals.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Numpy rec array from which I would like to do some
I have a 2D numpy array. Some of the values in this array are
I have a numpy array that contains some image data. I would like to
How can I remove some specific elements from a numpy array? Say I have
I have a numpy array like this: x = np.array([[1,2,3],[4,5,6],[7,8,9]]) I need to create
I have a numpy array like this a = np.array(1) Now if I want
I have some python code which generates a 256^3 numpy array of data that
I have an NxM array in numpy that I would like to take the
I have a Numpy array and a list of indices whose values I would
I have a 2D numpy array... there are some values in the image and

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.