0

I'm currently working on something were I need to find groups of values in a 2d list that are surrounded by another value and then change the values of the surrounded elements. Start/end of a sub-list counts as surrounded.

For exemple if I have this list :

   [[1, 2, 1, 1, 1, 0, 0, 0, 0],
    [2, 2, 2, 2, 2, 1, 0, 0, 0],
    [1, 2, 2, 1, 1, 0, 0, 0, 0],
    [0, 1, 1, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 1, 0, 0, 0, 0, 1, 0],
    [0, 1, 2, 1, 0, 0, 1, 1, 0],
    [0, 0, 1, 0, 0, 0, 1, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0]]

I want the function to change it to this:

   [[1, 3, 1, 1, 1, 0, 0, 0, 0],
    [3, 3, 3, 3, 3, 1, 0, 0, 0],
    [1, 3, 3, 1, 1, 0, 0, 0, 0],
    [0, 1, 1, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 1, 0, 0, 0, 0, 1, 0],
    [0, 1, 3, 1, 0, 0, 1, 1, 0],
    [0, 0, 1, 0, 0, 0, 1, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0]]

I have no idea how to do it. Can anyone please help me ?

Galou
  • 13
  • 4
  • So you just want to increment the `2`s to `3`s? – Mr. Polywhirl Apr 12 '21 at 13:32
  • I'm assuming that the rule here is that your groups of `2` values are surrounded on the side or top by `1` values (and diagonals don't count). Is that correct? And if a group of `2` values meets this rule then we change them to `3`? Feels a bit like minesweeper but with less rules. – JNevill Apr 12 '21 at 13:32
  • Why are the `2`s incremented to `3` when they are not completely surrounded by `1` (left side is open). Unless the start/end of a sub-array counts as a surrounding? You need to provide an alternative example, or clarify. – Mr. Polywhirl Apr 12 '21 at 13:36
  • Yes I want to change the `2` values that are surrounded by `1` values. But I don't know how to do it because some `2` values aren't directly surrounded by `1` values (like the top left group) No the left side isn't open. start/end of a sub list counts as surrounding. – Galou Apr 12 '21 at 13:51

1 Answers1

0
lst = [
    [1, 2, 1, 1, 1, 0, 0, 0, 0],
    [2, 2, 2, 2, 2, 1, 0, 0, 0],
    [1, 2, 2, 1, 1, 0, 0, 0, 0],
    [0, 1, 1, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 1, 0, 0, 0, 0, 1, 0],
    [0, 1, 2, 1, 0, 0, 1, 1, 0],
    [0, 0, 1, 0, 0, 0, 1, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0],
]

for row in range(len(lst)):
    for col in range(len(lst[row])):
        if lst[row][col] == 1:
            continue
        v1 = lst[row][col - 1] >= 1 if col - 1 >= 0 else 1
        v2 = lst[row - 1][col] >= 1 if row - 1 >= 0 else 1
        v3 = lst[row][col + 1] >= 1 if col + 1 < len(lst[row]) else 1
        v4 = lst[row + 1][col] >= 1 if row + 1 < len(lst) else 1
        if v1 + v2 + v3 + v4 == 4:
            lst[row][col] += 1

from pprint import pprint

pprint(lst)

Prints:

[[1, 3, 1, 1, 1, 0, 0, 0, 0],
 [3, 3, 3, 3, 3, 1, 0, 0, 0],
 [1, 3, 3, 1, 1, 0, 0, 0, 0],
 [0, 1, 1, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 1, 0, 0, 0, 0, 1, 0],
 [0, 1, 3, 1, 0, 0, 1, 1, 0],
 [0, 0, 1, 0, 0, 0, 1, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0, 0]]
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91