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

前端 未结 5 1092
离开以前
离开以前 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 15:01

    You need to read all lines of the file, perhaps with File.readlines(), or a loop like you already have, and then for each line read the number (if it is a number) and compare to the "best so far" value.

    It looks like you don't really need split(). What you do need to do, is check if each lines starts with a digit. If so, you can get the number with float(line). Maybe float(line.strip()) if whitespace is causing trouble. If the line doesn't start with a digit, keep it in a temporary variable. If the next line proves to offer a lower number than the best-so-far value, you can copy that temporary value into a variable for the tentative output.

提交回复
热议问题