Simple questions on use of objects (probably faq)

Max M maxm at mxm.dk
Wed Mar 8 07:21:11 EST 2006


Brian Elmegaard wrote:
> "Matt Hammond" <matt.hammond at rd.bbc.co.uk> writes:
> 
> 
>>y_max = max([e.x for e in y])
> 
> 
> Would there be a way to refer back to the e with maximum x, or how
> could I find other attributes of it?


In that case a common idiom is to "decorate"


decorated = [(obj.x, obj) for obj in objects]
max_decorated = max(decorated)
max_obj = max_decorated[-1]


Or to run through it "old style"

max_obj = objects[0]
for obj in objects:
     if obj.x > max_obj.x:
         max_obj = obj


Readbility is about the same I think. Testing should tell you which is 
faster in your case.

-- 

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science



More information about the Python-list mailing list