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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:57:59+00:00 2026-06-16T21:57:59+00:00

I’m looking for a Numpy (i.e. hopefully faster) way to perform the following: import

  • 0

I’m looking for a Numpy (i.e. hopefully faster) way to perform the following:

import numpy as np                                                              

x = np.array([1,2,3,4,5],dtype=np.double)                                       
arr = [[1,2],[0,4,3],[1,4,0],[0,3,4],[1,4]]                                 

ans = np.array([ x[item] - x[i] for i, item in enumerate(arr) ])

I’d like to get rid of the list comprehension and do this something like this (although, I know this won’t work)

x[arr[:]] - x[:]

arr is always a nested list of integers with length equal the length of x. The interior lists are not necessarily the same length (i.e. arr is a ragged list)

  • 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-16T21:58:00+00:00Added an answer on June 16, 2026 at 9:58 pm

    I’ve come up with a solution that is adequate enough for my application using Numpy masked arrays. In my application, the arr list is not “too ragged” (i.e. the max length of any interior list is not extremely different from the min length of any interior list). Therefore, I start by padding arr with -1s, then create a mask based on the location of the -1s. I perform my operation and use the mask on the resulting array. In this case, there are a few extra calculations being done unnecessarily (on the padded entries), but this is still faster that the Python loop (by a factor of almost 2). The example code is below:

    import numpy as np                                                              
    import numpy.ma as ma
    
    x = np.array([1,2,3,4,5],dtype=np.double)                                       
    arr = [[1,2],[0,4,3],[1,4,0],[0,3,4],[1,4]]                                     
    
    max_arr_length = max([ len(item) for item in arr ])                          
    
    arr_padded = [ np.pad(i,(0,max_arr_length-len(i)), mode='constant', 
        constant_values=-1) for i in arr ]
    arr_masked = ma.masked_equal(arr_padded,-1)
    
    ans_masked = ma.masked_array(x[arr_masked] - x[:, None], mask=arr_masked.mask)
    

    This is a bit of a hack, but it works well enough for me. It would be nice if Numpy had support for ragged arrays.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have an array which has BIG numbers and small numbers in it. I
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I am confused How to use looping for Json response Array in another Array.
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

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.