Python组连接值

时间:2017-11-28 14:28:43

标签: python grouping

我有一个这样的清单:

fastcgi_read_timeout = 2000;     # 2000 secs, or at least greater than the previous twos

如何对所有连接的邻居进行分组?

我可以找到所有个人" O"就像这样,

max_execution_time

让我这样:var myDropzone = new Dropzone("#uploader", { // ... timeout: "2100", // ... });

现在我想按连接项进行分组:

我的预期输出是:board = [["X", "X", "X", "X"], ["X", "O", "O", "X"], ["X", "X", "O", "X"], ["X", "O", "X", "X"]]

我不知道如何处理这个问题,一直在看all_o = [ (x, y) for x, row in enumerate(board) for y, col in enumerate(row) if col == 'O' ] ,这是我可以在这里使用的东西吗?

2 个答案:

答案 0 :(得分:2)

不确定是否有内置函数,但这是一个简单的强力算法,可以帮助您入门:

编辑:重写蛮力代码以处理凹组。

import numpy as np

board = [[0, 0, 0, 0],
 [0, 1, 0, 1],
 [0, 1, 1, 1],
 [0, 0, 0, 0],
 [1, 1, 0, 0]]

board = np.array(board)
board = np.pad(board,(1,1),'constant',constant_values=(0,0))
tt = np.where(board == 1)
idx_match = zip(tt[0],tt[1])

label_mask = np.zeros(board.shape)
labels = []
labels.append(1)
label_mask[idx_match[0]] = 1#initial case

#this loop labels convex clusters, but can't tell if they are touching
for i in idx_match[1:]:
    pts = [(i[0],i[1]-1),(i[0]-1,i[1]),(i[0],i[1]+1),(i[0]+1,i[1])]
    idx = []
    for pt in pts:
        if label_mask[pt] in labels:
            idx.append(labels.index(label_mask[pt]))

    if idx:
        idx = min(idx)
        label_mask[i] = labels[idx]
    else:
        labels.append(labels[-1] + 1)
        label_mask[i] = labels[-1]

#this loop goes through detected clusters and groups them together if adjacent
for lab in labels:
    adj_vals = []
    tt = np.where(label_mask == lab)
    idx = zip(tt[0],tt[1])

    for i in idx:
        pts = [(i[0],i[1]-1),(i[0]-1,i[1]),(i[0],i[1]+1),(i[0]+1,i[1])]
        adj_vals.extend([label_mask[pt] for pt in pts if label_mask[pt] != 0])

    adj_vals = list(set(adj_vals))

if adj_vals:
    label_mask[np.isin(label_mask,adj_vals)] = adj_vals[0]

label_mask = label_mask[1:-1,1:-1]#remove padding
print(label_mask)

这给了我们:

[[ 0.  0.  0.  0.]
 [ 0.  1.  0.  1.]
 [ 0.  1.  1.  1.]
 [ 0.  0.  0.  0.]
 [ 3.  3.  0.  0.]]

(预编辑,(0,3)点标记为2

答案 1 :(得分:1)

如何进行深度优先搜索以查找所有连接的组件?

def dfs(board, start):
    visited, stack = set(), [start]
    while stack:
        vertex = stack.pop()
        cur_i, cur_j = vertex[0], vertex[1]

        if vertex not in visited:
            visited.add(vertex)
            for i,j in [(-1,0),(1,0),(0,-1),(0,1)]:
                if (cur_i+i, cur_j+j) not in visited and 0 <= cur_i+i < len(board) and 0 <= cur_j+j < len(board[0]) and board[cur_i+i][cur_j+j] == "O":
                    stack.append((cur_i+i, cur_j+j))
    return list(visited)

board = [["X", "X", "X", "X"],
         ["X", "O", "O", "X"],
         ["X", "X", "O", "X"],
         ["X", "O", "X", "X"]]

all_o = [(x, y) for x, row in enumerate(board) for y, col in enumerate(row) if col == 'O']

res = []
done = set()
for cur in all_o:
    if cur not in done:
        comps = dfs(board, cur)
        done |= set(comps)
        res.append(comps)
print(res)

输出:

[[(1, 2), (1, 1), (2, 2)], [(3, 1)]]

参考:我从this blog post修改了DFS代码。