Python Forum
Identifying the value of all adjacent elements in an array
Thread Rating:
  • 3 Vote(s) - 2.67 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Identifying the value of all adjacent elements in an array
#1


Consider the following array:

Output:
m = [['*' '*' '*' '*' '*'] ['*' '*' '*' '*' '*'] ['*' 'O' '*' '*' '*'] ['*' '*' '*' '*' '*'] ['*' '*' '*' '*' '*']]
I want to be able to identify if any elements that are horizontal or vertical neighbours to 'O' (not diagonal neighbours) are also of value 'O'.

So far, I can only think of a rather brute force method, whereby I construct if statements using all possible combinations of up, down, left and right. As an example:

l = []
x = 1
y = 2
#(It is a char array, which is why the y and x are that way around in the indexing.)
if m[y + 1, x] and m[y-1, x] and [y, x - 1] and [y, x + 1] == 'O': #Check if all 4 neighbours are 'O'
    l.append((x, y + 1))
    l.append((x, y - 1))
    l.append((x + 1, y))
    l.append((x - 1, y))
elif m[y + 1, x] and m[y - 1, x] and [y, x - 1] == 'O': #Check if only some combination of 3 neighbours are 'O'
    l.append((x, y + 1))
    l.append((x, y - 1))
    l.append((x - 1, y))
elif m[y + 1, x] and m[y - 1, x] = 'O': #Check if only some combination of 2 neighbours are 'O'
    l.append((x, y + 1))
    l.append((x, y - 1))
elif m[y + 1, x] = 'O': #Check if only one of the neighbours is 'O'
    l.append((x, y + 1))
...
...
...
There are 20 possible combinations, so this would obviously take ages to write out and is unacceptable practice.

Does anyone have any ideas of a method to check if all horizontal and vertical neighbours of 'O' are also 'O', and if so then append to the list?
Reply
#2
see: https://stackoverflow.com/questions/6521...onal-array
for one possible solution
Reply
#3
This function is used to compute a list of horizontal, vertical and diagonal neighbours in a 10x10 array:

X = 10
Y = 10
neighbours = lambda x, y : [(x2-1, y2-1) for x2 in range(x-1, x+2) for y2 in range(y-1, y+2) if (-1 < x <= X+1 and -1 < y <= Y+1 and (x != x2 or y != y2) and (0 <= x2 <= X+1) and (0 <= y2 <= Y+1))]
However, I don't want to see the diagonal neighbours. How can I modify this code so that it only considers the horizontal and vertical neighbours?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to remove some elements from an array in python? gohanhango 9 1,140 Nov-28-2023, 08:35 AM
Last Post: Gribouillis
  Identifying if the program I have is python and then decompiling jpnyc 7 2,307 Jun-02-2022, 10:16 PM
Last Post: jpnyc
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,116 May-17-2022, 11:38 AM
Last Post: Larz60+
  Replace elements of array with elements from another array based on a third array Cola_Reb 6 1,834 May-13-2022, 06:06 PM
Last Post: deanhystad
Question Change elements of array based on position of input data Cola_Reb 6 2,112 May-13-2022, 12:57 PM
Last Post: Cola_Reb
  Identifying keywords in text drchips 6 91,768 Mar-29-2022, 12:32 PM
Last Post: snippsat
  trying to put a a filter on identifying a straight CompleteNewb 1 1,656 Dec-01-2021, 11:11 PM
Last Post: CompleteNewb
  Sorting Elements via parameters pointing to those elements. rpalmer 3 2,585 Feb-10-2021, 04:53 PM
Last Post: rpalmer
  Determine number of all the immediately adjacent points in python zackk 1 1,860 Feb-06-2021, 09:23 AM
Last Post: zackk
  4D array with only elements on one side of the diagonal schniefen 0 1,683 Dec-24-2020, 11:32 AM
Last Post: schniefen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020