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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:12:34+00:00 2026-06-15T15:12:34+00:00

I have a 2d array with a different species in each one. I pick

  • 0

I have a 2d array with a different species in each one. I pick a random element on the array and I want to count up how many of each species are in the eight squares immediately adjacent to that element.

But I want the array to wrap at the edges, so if I pick an element on the top row, the bottom row will be counted as “adjacent”. How can I do this while iterating through j in range (x-1,x+1) and the same for j and y?

Also, is there a more elegant way of omitting the element I originally picked while looking through the adjacent squares than the if (j!=x or k!=y line?

numspec = [0] * len(allspec)
for i in range (0,len(allspec)):
    #count up how many of species i there is in the immediate area
    for j in range(x-1,x+1):
        for k in range(y-1,y+1):
            if (j!=x or k!=y):
                numspec[hab[i][j]] = numspec[hab[i][j]]+1
  • 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-15T15:12:35+00:00Added an answer on June 15, 2026 at 3:12 pm

    As for wrapping, I would recomend using relative indexing from -1 to +1 and then computing real index using modulo operator (%).

    As for making sure you don’t count the original element (x, y), you are doing just fine (I would probably use reversed contidion and continue, but it doesn’t matter).

    I don’t quite understand your usage of i, j, k indexes, so I’ll just assume that i is index of the species, j, k are indexes into the 2d map called hab which I changed to x_rel, y_rel and x_idx and y_idx to make it more readable. If I’m mistaken, change the code or let me know.

    I also took the liberty of doing some minor fixes:

    • introduced N constant representing number of species
    • changed range to xrange (xrange is faster, uses less memory, etc)
    • no need to specify 0 in range (or xrange)
    • instead of X = X + 1 for increasing value, I used += increment operator like this: X += 1

    Here is resulting code:

    N = len(allspec)
    numspec = [0] * N
    for i in xrange(N):
        for x_rel in xrange(-1, +1):
            for y_rel in xrange(-1, +1):
                x_idx = (x + xrel) % N
                y_idx = (y + yrel) % N
                if x_idx != x or y_idx != y:
                    numspec[hab[x_idx][y_idx]] += 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have array of countries. I want to pick 5 random countries from my
I have an array of strings, each one with a different length. e.g: s[0]
I have a 4D array of measurements in MATLAB. Each dimension represents a different
I have two different arrays. One array, a, for a list of people. My
I have two different arrays, one with strings and another with ints. I want
I have an array $rows where each element is a row of 15 tab-delimited
I want to have an array of objects that is shared between two different
So i have a string array full of different vodkas, what i want to
I have to check each xml file inside an array of different directories. My
I have an array of different objects with different data types I want to

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.