Python, lambda, find minimum

Posted by qba on Stack Overflow See other posts from Stack Overflow or by qba
Published on 2009-10-27T22:37:03Z Indexed on 2010/06/16 18:42 UTC
Read the original article Hit count: 153

Filed under:
|
|

I have foreach function which calls specified function on every element which it contains. I want to get minimum from thise elements but I have no idea how to write lambda or function or even a class that would manage that. Thanks for every help.


I use my foreach function like this:

o.foreach( lambda i: i.call() )

or

o.foreach( I.call )

I don't like to make a lists or other objects. I want to iterate trough it and find min.

I manage to write a class that do the think but there should be some better solution than that:

class Min:                                           
    def __init__(self,i):                        
        self.i = i                              
    def get_min(self):                               
        return self.i                                
    def set_val(self,o):                             
        if o.val < self.i: self.i = o.val

m = Min( xmin )
self.foreach( m.set_val )                            
xmin = m.get_min()


Ok, so I suppose that my .foreach method is non-python idea. I should do my Class iterable because all your solutions are based on lists and then everything will become easier.

In C# there would be no problem with lambda function like that, so I though that python is also that powerful.

© Stack Overflow or respective owner

Related posts about python

Related posts about beginner