Jump to content

Python - How to find cells around index in Array?

Yellow_
Go to solution Solved by Guest,
1 minute ago, Yellow_ said:

I have given that a try, though array indexing is a little weirder than list indexing it seems. The edges really goof me up 

You can't access things outside the bounds of the array.


You can use error handling like "try catch" if python has that.


Or you can use if statements.
IE

if (!(index - 1 < 0))
	Do code
if (!(index + 1 > highestIndex))
	Do code

 

Say you have an array- and it looks like this

[[1,2,3]
[4,5,6]
[7,8,9]]

If at 5, how would I be able to get all the surrounding items into a list? Or even the surrounding indexes of items?

 

alternatively, if it was at the index of 4, how would I get the adjacent ones for that? Because of the missing left side?

Link to comment
Share on other sites

Link to post
Share on other sites

It's probably best to use a 2D array for this.

I don't know Python, so excuse me while I use some 'pseudo code' which can be translated in the correct python function. Because I don't know Python very well; I don't know for sure what you posted is or is not a 2D array already.

 

So what you want it to do is look at a value in the array and see what values surround that?

e.g. if you select the value '5' you want to retrieve 2, 4, 6 and 8 in this case?

The 4 and 6 are easy to get; just get the index of the number 5 (which would be [1][1]) and just have that minus 1 and plus 1 to get the index of 4 and 6 (to get [1][0] and [1][2], which are the positions of those numbers in a 2D array).

 

2 and 8 are also somewhat easy; just do the same as before, but do it with the first value of the 2D array, so you get [0][1] and [2][1].

 

For the instances like numbers 1, 4, 7, other numbers on the side and bottom and such.. You just have to make a system that checks if the value is at the edge of the array, which you can do by simply check if the array value is on the edge, which in this case would be if the first value in the array is either 0, or 2 (instead of 2, you should get the length of the first part of the array, that's better because you can increase the size of the array) and/or the second letter is either 0 or 2 (same story for the number 2).

 

So, what I'm saying is this:

- Make a 2D array for this data

- When you look up a value, save the position in the array.

- Add and subtract 1 in the first half of the 2D array for the numbers above and below

- Add and subtract 1 in the second half of the 2D array for the number left and right

- Write the exceptions, so it doesn't subtract 1 when you're already on 0, and doesn't add 1 when you're at 2 (length of array)

Return the values afterwards.

 

 Note:

If you actually wanted to return the corner numbers too, so for 5 that would be 1, 3, 7, 9, you just need to add a couple extra steps to find those too.

Also, remember how arrays works; they start at index 0. Meaning in your array the value "1" is actually index 0, value "2" = index 2, value "9" = index 8 ,etc.

 

I hope this helps you out a bit in finding a solution for what you're trying to do, if it doesn't or if anything is unclear; please do quote me and ask.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

The simple answer is: Don't use a 2D array. Use a 1 dimensional array. Then you can just return index -1, index, and index + 1.

 

To do this with a "2D" array, in python, a list of lists, things are slightly more complicated, as you will have to check if you are on the edges of one of the inner arrays. In both cases, there are two edge cases, the first item in the list, and the last item in the list.

 

In python, simply doing index - 1 when index = 0 will return the last element of the array, and doing index + 1 when index = list.length will raise an exception.

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

You could get the index of your current number then 

get index plus 1

get index minus 1

then do that for all the other dimensions. 

Link to comment
Share on other sites

Link to post
Share on other sites

On 4/3/2019 at 3:40 AM, Minibois said:

 

Yes that was a 2D array already, though I'll take what you said into consideration. I've tried a lot of it already but I see some other routes to go based on what you said

 

On 4/3/2019 at 4:44 AM, straight_stewie said:

 

Referencing the edges, I had an idea for it- would I be able to add an extra "layer" of cells around the array of say "B", so then the actual edges would just be "B" instead of a nonexistant index?

 

On 4/3/2019 at 7:25 AM, fpo said:

 

I have given that a try, though array indexing is a little weirder than list indexing it seems. The edges really goof me up

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Yellow_ said:

I have given that a try, though array indexing is a little weirder than list indexing it seems. The edges really goof me up 

You can't access things outside the bounds of the array.


You can use error handling like "try catch" if python has that.


Or you can use if statements.
IE

if (!(index - 1 < 0))
	Do code
if (!(index + 1 > highestIndex))
	Do code

 

Link to comment
Share on other sites

Link to post
Share on other sites

On 4/3/2019 at 12:49 PM, fpo said:

if (!(index - 1 < 0)) Do code if (!(index + 1 > highestIndex)) Do code

So in this case, would I just write specific code for the edge locations?

Just slightly confused on how this works

Thanks

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Yellow_ said:

So in this case, would I just write specific code for the edge locations?

Just slightly confused on how this works

Thanks

Yeah, so before you access the indexes outside the array, make sure they are within the array’s bounds with the if statement. 

Link to comment
Share on other sites

Link to post
Share on other sites

It thought I would have a go, do try to complete it before looking at my code. It's not fun just to copy :P

 

Spoiler

grid = [
  [1,2,3],
  [4,5,6],
  [7,8,9]
]

def get_around(row, col):
  # in python lists start at 0
  #-1 so we can input as if starting from one
  r = row -1
  c = col -1
  output = []
  # loop the grid 
  for idx, row in enumerate(grid):
    # append all the values in the same coll
    output.append(row[c])
    #if the rows are the same get the value before and after
    if idx == r:
      #if c is > 0 then we can get a value before
      if c > 0:
        output.append(row[c-1])
      # if c < length of the row -1 then we can get value after
      if c < len(row)-1:
        output.append(row[c+1])
  # return our list
  return output
  

close_values  = get_around(1,3)
print(close_values)
#[3,2,6,9]

 

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×