As you've read from a file your list will be full of strings. You need to convert these to ints/floats otherwise max/min
will not return the max/min
numerical values. The code below will convert each value in data_list
to an integer using a list comprehension and then return the maximum value.
max_value = max([int(i) for i in data_list])
You could do this before the fact so you don't have to convert it again for min
:
with open(data_file, 'r') as f:
data_list = [int(i) for i in f.readlines()]
max_value = max(data_list)
min_value = min(data_list)
I am using python 2.7 to find highest and anycodings_text-files lowest values in an text file. ,The text file is simply formatted with one anycodings_text-files Integer value at each line. The following anycodings_text-files code collects the numbers from the file:,Then I try to reach for the maximum and anycodings_text-files minimum values with:,I feel that there might be something anycodings_text-files fundamentally wrong in my understanding of anycodings_text-files python, please help me pinpoint my anycodings_text-files misconception.
The text file is simply formatted with one anycodings_text-files Integer value at each line. The following anycodings_text-files code collects the numbers from the file:
with open(data_file, 'r') as f:
data_list = f.readlines()
Then I try to reach for the maximum and anycodings_text-files minimum values with:
max_value = max(data_list)
min_value = min(data_list)
print('Max value: ' + str(max_value) + ' Min value: ' + str(min_value))
As you've read from a file your list anycodings_iteration will be full of strings. You need to anycodings_iteration convert these to ints/floats otherwise anycodings_iteration max/min will not return the max/min anycodings_iteration numerical values. The code below will anycodings_iteration convert each value in data_list to an anycodings_iteration integer using a list comprehension and anycodings_iteration then return the maximum value.
max_value = max([int(i) for i in data_list])
You could do this before the fact so you anycodings_iteration don't have to convert it again for min:
with open(data_file, 'r') as f:
data_list = [int(i) for i in f.readlines()]
max_value = max(data_list)
min_value = min(data_list)
The Excel MAX function returns the largest numeric value in the data provided. MAX ignores empty cells, the logical values TRUE and FALSE, and text values.,As the formula is copied down the column, E5 increments at each row, so that MAX and IF return a new array. The MAX function continues to return the largest value in each array — the nth value in the series.,Once we have the largest value established, we create another formula that simply checks all values in the named range "rng" against the "last largest value":,To get the nth largest value in a data set with duplicates, you can use an array formula based on the MAX and IF functions.
= MAX(IF(rng < A2, rng))
In the example shown, the formula in E6 is:
= MAX(IF(rng < E5, rng))
= MAX(IF(rng < E5, rng))
= MAX(IF(rng < E5, rng))
First, we get the largest value using the MAX function in E5:
= MAX(rng)
= MAX(rng)
Once we have the largest value established, we create another formula that simply checks all values in the named range "rng" against the "last largest value":
= MAX(IF(rng < E5, rng))
Any value that is lower than the "last largest" survives the test, and any value that's not ends up FALSE. The resulting array looks like this:
{ 12; FALSE; FALSE; 11; 12; 12; 10 }
{ 12; FALSE; FALSE; 11; 12; 12; 10 }
= MAX(rng)
{ 12; FALSE; FALSE; 11; 12; 12; 10 }