在二维数组 (Python) 中找到所有相似的 "touching" 元素
Locate all similar "touching" elements in a 2D Array (Python)
假设我有数组:
someArray = [["0","1","1","0"]
["0","1","0","1"]
["0","1","0","1"]
["0","1","1","0"]]
我想指出数组中的一个元素,然后能够识别每个相似的 "touching" 元素(感人的意思是,如果将数组视为一个网格,它们将通过一个或多个连接起来连接)。例如,在这种情况下,如果我选择 someArray[0][0],它会给我 [1][0]、[2][0] 和 [3][0],因为所有这些元素都是“ 0”,彼此 "touching"。我只是指触摸 NESW,没有上述方向的组合。
我需要做什么才能开始处理这个问题?
编辑:原来只是 "Flood Fill"。
您可以考虑学习如何实施 breadth-first searches and depth-first searches 以完成您的 objective。以下示例显示了如何在一个函数中轻松处理这两种搜索策略。模块化方法应该使代码易于更改。
#! /usr/bin/env python3
from collections import deque
from operator import eq
def main():
"""Show how to search for similar neighbors in a 2D array structure."""
some_array = ((0, 1, 1, 0),
(0, 1, 0, 1),
(0, 1, 0, 1),
(0, 1, 1, 0))
neighbors = (-1, 0), (0, +1), (+1, 0), (0, -1)
start = 0, 0
similar = eq
print(list(find_similar(some_array, neighbors, start, similar, 'BFS')))
def find_similar(array, neighbors, start, similar, mode):
"""Run either a BFS or DFS algorithm based on criteria from arguments."""
match = get_item(array, start)
block = {start}
visit = deque(block)
child = dict(BFS=deque.popleft, DFS=deque.pop)[mode]
while visit:
node = child(visit)
for offset in neighbors:
index = get_next(node, offset)
if index not in block:
block.add(index)
if is_valid(array, index):
value = get_item(array, index)
if similar(value, match):
visit.append(index)
yield node
def get_item(array, index):
"""Access the data structure based on the given position information."""
row, column = index
return array[row][column]
def get_next(node, offset):
"""Find the next location based on an offset from the current location."""
row, column = node
row_offset, column_offset = offset
return row + row_offset, column + column_offset
def is_valid(array, index):
"""Verify that the index is in range of the data structure's contents."""
row, column = index
return 0 <= row < len(array) and 0 <= column < len(array[row])
if __name__ == '__main__':
main()
假设我有数组:
someArray = [["0","1","1","0"]
["0","1","0","1"]
["0","1","0","1"]
["0","1","1","0"]]
我想指出数组中的一个元素,然后能够识别每个相似的 "touching" 元素(感人的意思是,如果将数组视为一个网格,它们将通过一个或多个连接起来连接)。例如,在这种情况下,如果我选择 someArray[0][0],它会给我 [1][0]、[2][0] 和 [3][0],因为所有这些元素都是“ 0”,彼此 "touching"。我只是指触摸 NESW,没有上述方向的组合。
我需要做什么才能开始处理这个问题?
编辑:原来只是 "Flood Fill"。
您可以考虑学习如何实施 breadth-first searches and depth-first searches 以完成您的 objective。以下示例显示了如何在一个函数中轻松处理这两种搜索策略。模块化方法应该使代码易于更改。
#! /usr/bin/env python3
from collections import deque
from operator import eq
def main():
"""Show how to search for similar neighbors in a 2D array structure."""
some_array = ((0, 1, 1, 0),
(0, 1, 0, 1),
(0, 1, 0, 1),
(0, 1, 1, 0))
neighbors = (-1, 0), (0, +1), (+1, 0), (0, -1)
start = 0, 0
similar = eq
print(list(find_similar(some_array, neighbors, start, similar, 'BFS')))
def find_similar(array, neighbors, start, similar, mode):
"""Run either a BFS or DFS algorithm based on criteria from arguments."""
match = get_item(array, start)
block = {start}
visit = deque(block)
child = dict(BFS=deque.popleft, DFS=deque.pop)[mode]
while visit:
node = child(visit)
for offset in neighbors:
index = get_next(node, offset)
if index not in block:
block.add(index)
if is_valid(array, index):
value = get_item(array, index)
if similar(value, match):
visit.append(index)
yield node
def get_item(array, index):
"""Access the data structure based on the given position information."""
row, column = index
return array[row][column]
def get_next(node, offset):
"""Find the next location based on an offset from the current location."""
row, column = node
row_offset, column_offset = offset
return row + row_offset, column + column_offset
def is_valid(array, index):
"""Verify that the index is in range of the data structure's contents."""
row, column = index
return 0 <= row < len(array) and 0 <= column < len(array[row])
if __name__ == '__main__':
main()