Search code examples
pythonprintingminimum

Finding minimum in outputs of a forloop


How can we find minimum value in output of a loop without creating a list?

for i in range(1,5):
    R1=np.random.uniform(0,10,i)
    def L():        
        d=R1**2
        return d
    print("i= ",i, min(L()))

this code gives minimum for each i:

i=1, 0.00477514033271
i=2, 5.65882743189
i=3, 0.783497908243
i=4, 0.224239938297

but the minimum value of the whole outputs is: 0.00477514033271

Thank yo for your help.


Solution

  • This is a textbook problem (If I understood your question correctly).

    import numpy as np
    
    # variable to keep track of the minimum seen so far
    temp = 1e10 # a big big big value
    for i in range(1,5):
        R1=np.random.uniform(0,10,i)
        def L():
            d=R1**2
            return d
        m = min(L())
        if m < temp:
            temp = m
        print("i= ",i, min(L()))
    
    print ("Min is :", temp)