Finding smallest float in file then printing that and line above it

前端 未结 5 1093
离开以前
离开以前 2020-11-29 14:16

My data file looks like this:

3.6-band 
6238
Over
0.5678
Over
0.6874
Over
0.7680
Over
0.7834

What I want to do is to pick out the smallest

5条回答
  •  隐瞒了意图╮
    2020-11-29 14:49

    I see some interesting solutions above. I would go for this straightforward solution. There is one problem left, which is that integers might be taken like this as well. Anyone a solution for this?

        df=open('myfile.txt')
        lines=df.readlines()
        minval = 1e99
        for n,line in enumerate(lines):
            try: 
                val=float(line)  # NB! like this, also integers will be taken. 
                if val < minval:  
                    minval = val
                    i_min  = n  
            except:
                pass
        word = lines[i_min-1]
    

提交回复
热议问题