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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:33:59+00:00 2026-05-25T17:33:59+00:00

I want to get the neighbors of the certain element in the numpy array.

  • 0

I want to get the neighbors of the certain element in the numpy array. Lets consider following example

    a = numpy.array([0,1,2,3,4,5,6,7,8,9])

So I want to specify position 5 and want to get three neighbors from both sides. It can be done

   index = 5
   num_neighbor=3
   left = a[index-num_neighbor:index]
   right= a[num_neighbor+1:num_neighbor+index+1]

The above code does not take care of the boundaries… I want that i get the neighbours within the boundaries of the array. For this consider the following example if index is 1 then the left neighbor is only one element which is 0.

Thanks a lot

  • 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-25T17:34:00+00:00Added an answer on May 25, 2026 at 5:34 pm
    import numpy as np
    a = np.array([0,1,2,3,4,5,6,7,8,9])
    num_neighbor=3    
    
    for index in range(len(a)):
        left = a[:index][-num_neighbor:]
        right= a[index+1:num_neighbor+index+1]
        print(index,left,right)
    

    yields

    (0, array([], dtype=int32), array([1, 2, 3]))
    (1, array([0]), array([2, 3, 4]))
    (2, array([0, 1]), array([3, 4, 5]))
    (3, array([0, 1, 2]), array([4, 5, 6]))
    (4, array([1, 2, 3]), array([5, 6, 7]))
    (5, array([2, 3, 4]), array([6, 7, 8]))
    (6, array([3, 4, 5]), array([7, 8, 9]))
    (7, array([4, 5, 6]), array([8, 9]))
    (8, array([5, 6, 7]), array([9]))
    (9, array([6, 7, 8]), array([], dtype=int32))
    

    The reason why a[index-num_neighbor:index] does not work when index<num_neighbor is because of slicing rules #3 and #4:

    Given s[i:j]:

    If i or j is negative, the index is relative to the end of the string:
    len(s) + i or len(s) + j is substituted.

    The slice of s from i to j is defined as the sequence of items with
    index k such that i <= k < j. If i or j is greater than len(s), use
    len(s). If i is omitted or None, use 0. If j is omitted or None, use
    len(s). If i is greater than or equal to j, the slice is empty.

    So when index=1, then a[index-num_neighbor:index] = a[-2:1] = a[10-2:1] = a[8:1] = [].

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

Sidebar

Related Questions

I want get all of the Geom objects that are related to a certain
I want get as much as possible from Redis + Hiredis + libevent. I'm
I just want get a 2 dimension array of List in c#. In my
I want to get show information from myspace artists. One way I could do
I want get only the starting html tags. Lets say I have html like
I want get integer quotient when I divide two values. Per example X=3 Y=2
I want get the most specific from the current url in visual basic .net.
i want get string from one view controller to another view controller i done
i want get the last item of an array with class names. My code
I want get the image from an xml file which is encoded using base64.

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.