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:
- write a function to check if a string can be converted to float, this can be done by using
try
statement andfloat()
- filter floats from lines read from file
- find the min and max values
- find indices of min and max in list of lines using
list.index(<value>)
- 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
Any Good Tools to Solve Integer Programs on Linux
How to Generate a Static HTML File from a Swagger Documentation
Can't Remove, Purge, Unistall Mongodb from Debian
Get Latest Release Version Number for Chrome Browser
Automatically Sync Two Amazon S3 Buckets, Besides S3Cmd
How to Exit The Wh Mode in Gdb
Use Conditional in Bash Script to Check String Argument
Extract Lines When Column K Is Empty with Awk/Perl
Can Not Connect to Google Compute Engine via Ssh
Do Different Programs Gets Their Memory from a Common Heap or from a Separate Heap
Reusing Custom Makefile for Static Library with Cmake
How to Search for a Particular String from a .Gz File
Why Doesn't Tar Preserve File Permissions
Linux: How to Debug a Sigsegv? How to Trace The Error Source
You Don't Have Permission Error in Apache in Centos