Python recursive tree, linked list thingy

Ian Kelly ian.g.kelly at gmail.com
Wed Mar 7 15:27:46 EST 2012


On Wed, Mar 7, 2012 at 1:03 PM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> A set of defective pixels would be the probable choice, since it
> offers efficient membership testing.

Some actual code, using a recursive generator:

def get_cluster(defective, pixel):
    yield pixel
    (row, column) = pixel
    for adjacent in [(row - 1, column), (row, column - 1),
                     (row, column + 1), (row + 1, column)]:
        if adjacent in defective:
            defective.remove(adjacent)
            for cluster_pixel in get_cluster(defective, adjacent):
                yield cluster_pixel


defective = {(327, 415), (180, 97), (326, 415), (42, 15),
             (180, 98), (325, 414), (325, 415)}
clusters = []

while defective:
    pixel = defective.pop()
    clusters.append(list(get_cluster(defective, pixel)))

from pprint import pprint
pprint(clusters)


Cheers,
Ian



More information about the Python-list mailing list