baffling bug in this bit of code!

Jonathan Hogg jonathan at onegoodidea.com
Thu Jul 4 05:51:43 EDT 2002


On 4/7/2002 7:48, in article
e7e4eac4.0207032248.7c988f2c at posting.google.com, "erik_w"
<erik.wilsher at iname.com> wrote:

> You might be better off with this snippet of code (if you run 2.1 or newer):
> 
> nodes=self.nodes
> locs_x = [nodes[n].location.x for n in nodes.keys()]
> locs_y = [nodes[n].location.y for n in nodes.keys()]
> max_x = max(locs_x)
> max_y = max(locs_y)
> 
> Shorter and probably faster.

Hmmm. Shorter, yes; faster, unlikely - since it now has four separate loops
and the creation of four intermediate lists.

If an intermediate list isn't a problem in space then I'd go for:

    reduce( lambda (x1,y1), (x2,y2): ( max(x1,x2), max(y1,y2) ),
            [ (node.location.x, node.location.y)
              for node in nodes.itervalues() ], (0, 0) )

which is certainly shorter ;-)  but breaking it out into an explicit loop
would probably be faster.

Jonathan




More information about the Python-list mailing list