Python recursive tree, linked list thingy

Wanderer wanderer at dialup4less.com
Thu Mar 8 11:12:56 EST 2012


On Mar 7, 3:27 pm, Ian Kelly <ian.g.ke... at gmail.com> wrote:
> On Wed, Mar 7, 2012 at 1:03 PM, Ian Kelly <ian.g.ke... 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

This works for me and I can modify it to look for column defects also.
It also shows I know less about Python then I thought I did. I had to
read up on generators and iterators to understand the code.

Thanks



More information about the Python-list mailing list