How to create sublists obeying the connection of cells in an array

1

Friends, I'm trying to create sublists from an array to target cells that are connected, such as the array below:

From this matrix I get this list with the painted cells:

lista = [[1, 1], [2, 1], [2, 2], [2, 3], [2, 5], [2, 6], [3, 2], [3, 5], [3, 6], [6,1], [6, 4], [6, 5], [7,1], [7, 4], [7, 5]]

I want as a result, sublists with yellow cells, grouped like this:

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

I do not know if it is possible, but so far, with the function below:

def group_by_diff(group, diff=1):
    container = []
    new_group = [group[0]] 
    for i in range(1,len(group)):
        if((group[i][0] - group[i-1][0] > diff) or (group[i][1] - group[i-1][1] > diff)):
            container.append(new_group)
            new_group = [group[i]]
        else:
            new_group.append(group[i])
    if(len(new_group) > 0):
        container.append(new_group)
    return container

I get this result only:

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

What do not answer, in this example have created 6 sublists, where in fact only 4 agglomerations should be created, according to the image.

I hope you have made me understand. Thank you.

    
asked by anonymous 20.02.2017 / 04:23

0 answers