How can this verbose, unpythonic routine be improved?

Posted by fmark on Stack Overflow See other posts from Stack Overflow or by fmark
Published on 2010-05-15T04:08:24Z Indexed on 2010/05/15 4:14 UTC
Read the original article Hit count: 325

Is there a more pythonic way of doing this? I am trying to find the eight neighbours of an integer coordinate lying within an extent. I am interested in reducing its verbosity without sacrificing execution speed.

def fringe8((px, py), (x1, y1, x2, xy)):
    f = [(px - 1, py - 1),
         (px - 1, py),
         (px - 1, py + 1),
         (px, py - 1),
         (px, py + 1),
         (px + 1, py - 1),
         (px + 1, py),
         (px + 1, py + 1)]
    f_inrange = []
    for fx, fy in f:
        if fx < x1:  continue
        if fx >= x2: continue
        if fy < y1:  continue
        if fy >= y2: continue
        f_inrange.append((fx, fy))
    return f_inrange

© Stack Overflow or respective owner

Related posts about python

Related posts about pythonic