Finding The Max and Min Values and Printing The Line from a File

Find the line with the min and max value and your line number from text file (Get Value Error Float Type)

One possible approach without the need for any extra modules

Code:

def is_float(x):
try:
float(x)
return True
except:
return False

with open('url1.txt', 'r') as myfile:
lines = myfile.readlines()

nums = [x for x in lines if is_float(x)]
my_min = min(nums)
my_max = max(nums)

print('Max: ', my_max, 'line number: ', lines.index(my_max)+1)
print()
print('Min: ', my_min, 'line number: ', lines.index(my_min)+1)

Input:

FOO >
1.0
BAR <
0.004
FOO FOO <
0.000004
BAR BAR <

Output:

Max:  1.0
line number: 2

Min: 0.000004
line number: 6

Explanation:

  1. write a function to check if a string can be converted to float, this can be done by using try statement and float()
  2. filter floats from lines read from file
  3. find the min and max values
  4. find indices of min and max in list of lines using list.index(<value>)
  5. Add 1 to indices to get line number as indices starts from zero

How to find the minimum and maximum value of the each row from the text file in file handling?

Loop over the lines, split. convert to int and use min / max

with open ('in.txt') as f:
data = []
for line in f:
numbers = [int(x) for x in line.strip().split()]
data.append((min(numbers),max(numbers)))
print(data)

output

[(2, 15), (4, 98), (11, 89)]

Get the max and min values from each row (unix)

I would put everything into a new file

awk  'NR>1{a=0; b=0; for (i=1;i<=NF;i++) if ($i < a || i == 1)a = $i; else if($i > b|| i == 1)b = $i; print $1,a, b}' test.txt > new_file2

NR>1 skips the first line which are columns name. Go through each row, save max and min and print the name, min, and max. Hope it helps.

My Output. The desired output you have above is wrong because the min of row CR_t1t is not 1.7.1234 like you have written but actually 2.4607684.

CR_A 0.58573062 4.1076807
LR_57 0.82643804 2.9050872
CR_t1t -0.56374447 2.4607684
D10 -0.4515 2.2971

Finding Min and Max numbers in a txt file

This loop reads all numbers in the file:

while (input>>number)
{
counter++;
sum=sum+number;
}

After that loop, input is already at the end of the file and the next loop will not read any numbers.

Read the numbers once and do all processing in a single loop. Alternatively, store the numbers in a std::vector and then work on that.

Moreoever, you never modify Min and Max in your code. What this loop:

while (input>>number)
{
if (number>Max)
number=Max;
else
number=Min;
}

does instead is to clip the numbers such that any number bigger than 0 (Max) is set to 0 and any other number is set to 0 (Min).

You probably want something along the line of

double number;
input >> number;
double min = number;
double max = number;
while ( input >> number) {
if (number > max) max = number;
if (number < min) min = number;
}

I used the first number to initialize min and max. If you don't to that, 0 is no good initial value for min and max, but you can use std::numerical_limits<double>::min as initial value for max and std::numerial_limits<double>::max as initial value for min.



Related Topics



Leave a reply



Submit