Python | Min/Max value in float string list
Last Updated :
08 Apr, 2023
Sometimes, while working with a Python list, we can have a problem in which we need to find min/max value in the list. But sometimes, we don’t have a natural number but a floating-point number in string format. This problem can occur while working with data, both in web development and Data Science domain. Let’s discuss a way in which this problem can be solved.
Method 1: Using min()/max() + float()
This problem can be solved using the min or max function in which we first convert the strings into float and then pass this logic in functions in respective min/max function.
Python3
test_list = [ '4.5' , '7.8' , '9.8' , '10.3' ]
print ( "The original list is : " + str (test_list))
res_min = min (test_list,key = lambda x: float (x))
res_max = max (test_list,key = lambda x: float (x))
print ( "The min value of list : " + str (res_min))
print ( "The max value of list : " + str (res_max))
|
Output
The original list is : ['4.5', '7.8', '9.8', '10.3']
The min value of list : 4.5
The max value of list : 10.3
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 2: Using the built-in reduce() function along with the lambda function to find the minimum and maximum values.
Using the built-in reduce() function along with the lambda function is one approach to finding the minimum and maximum values in a list of float strings. The reduce() function applies a given function to the elements of an iterable, cumulatively reducing them to a single value. By passing in a lambda function that compares the current minimum or maximum value to the next element in the list, the reduce() function can be used to find the minimum or maximum value.
Python3
from functools import reduce
test_list = [ '4.5' , '7.8' , '9.8' , '10.3' ]
res_min = reduce ( lambda x, y: x if float (x) < float (y) else y, test_list)
res_max = reduce ( lambda x, y: x if float (x) > float (y) else y, test_list)
print ( "The min value of list : " + str (res_min))
print ( "The max value of list : " + str (res_max))
|
Output
The min value of list : 4.5
The max value of list : 10.3
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #3: Use a for loop and convert each element to float before comparing them.
Step-by-step approach:
- Initialize variables for minimum and maximum values to None.
- Iterate through the list using a for loop.
- Convert each element to float using the float() function.
- Compare the float value to the current minimum value. If the value is less than the current minimum value or if the minimum value is None, update the minimum value.
- Compare the float value to the current maximum value. If the value is greater than the current maximum value or if the maximum value is None, update the maximum value.
- After iterating through the entire list, the minimum and maximum values will be stored in their respective variables.
- Print the minimum and maximum values.
Below is the implementation of the above approach:
Python3
test_list = [ '4.5' , '7.8' , '9.8' , '10.3' ]
print ( "The original list is : " + str (test_list))
min_val = None
max_val = None
for elem in test_list:
float_elem = float (elem)
if min_val is None or float_elem < min_val:
min_val = float_elem
if max_val is None or float_elem > max_val:
max_val = float_elem
print ( "The min value of list : " + str (min_val))
print ( "The max value of list : " + str (max_val))
|
Output
The original list is : ['4.5', '7.8', '9.8', '10.3']
The min value of list : 4.5
The max value of list : 10.3
Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(1), as we only need to store two variables for the minimum and maximum values.
Method 5: Using numpy library
step-by-step approach :
- Import the numpy library using the statement import numpy as np.
- Define the input list test_list with string elements as [‘4.5’, ‘7.8’, ‘9.8’, ‘10.3’].
- Convert the input list to a numpy array of float elements using the np.array() method and assign it to a variable float_arr. The dtype parameter is set to float to ensure that the elements are of float data type.
- Use the np.min() function to get the minimum value from the float_arr array and assign it to a variable min_val.
- Use the np.max() function to get the maximum value from the float_arr array and assign it to a variable max_val.
- Print the result using the print() function. The minimum value and maximum value are printed using the variables min_val and max_val respectively.
Python3
import numpy as np
test_list = [ '4.5' , '7.8' , '9.8' , '10.3' ]
float_arr = np.array(test_list, dtype = float )
min_val = np. min (float_arr)
max_val = np. max (float_arr)
print ( "The min value of list : " + str (min_val))
print ( "The max value of list : " + str (max_val))
|
OUTPUT:
The min value of list : 4.5
The max value of list : 10.3
The time complexity of this method is O(n) to convert the string list to a numpy array, and O(1) to get the minimum and maximum values using numpy functions. So the overall time complexity is O(n).
Auxiliary space: This method requires extra space to store the numpy array, which has a space complexity of O(n).
Similar Reads
Python | Max/Min value in Nth Column in Matrix
Sometimes, while working with Python Matrix, we may have a problem in which we require to find the minimum and maximum value of a particular column. This can have a possible application in day-day programming and competitive programming. Let's discuss certain ways in which this task can be performed
7 min read
How to use String Formatters in Python
In Python, we use string formatting to control how text is displayed. It allows us to insert values into strings and organize the output in a clear and readable way. In this article, weâll explore different methods of formatting strings in Python to make our code more structured and user-friendly. U
3 min read
How to find the int value of a string in Python?
In Python, we can represent an integer value in the form of string. Int value of a string can be obtained by using inbuilt function in python called as int(). Here we can pass string as argument to this function which returns int value of a string. int() Syntax : int(string, base) Parameters : strin
1 min read
Python | Find maximum value in each sublist
Finding the maximum value in each sublist involves iterating through a list of lists and identifying the largest element within each sublist. This operation is useful for analyzing data sets, and extracting the most significant values from grouped information. In this article, we will explore method
2 min read
Use of min() and max() in Python
Prerequisite: min() max() in Python Let's see some interesting facts about min() and max() function. These functions are used to compute the maximum and minimum of the values as passed in its argument. or it gives the lexicographically largest value and lexicographically smallest value respectively,
2 min read
Check If Value Is Int or Float in Python
In Python, you might want to see if a number is a whole number (integer) or a decimal (float). Python has built-in functions to make this easy. There are simple ones like type() and more advanced ones like isinstance(). In this article, we'll explore different ways to check if a value is an integer
4 min read
Convert String to Float in Python
The goal of converting a string to a float in Python is to ensure that numeric text, such as "33.28", can be used in mathematical operations. For example, if a variable contains the value "33.28", we may want to convert it to a float to perform operations like addition or division. Let's explore dif
2 min read
max() and min() in Python
This article brings you a very interesting and lesser-known function of Python, namely max() and min(). Now when compared to their C++ counterpart, which only allows two arguments, that too strictly being float, int or char, these functions are not only limited to 2 elements, but can hold many eleme
3 min read
Python | Set 3 (Strings, Lists, Tuples, Iterations)
In the previous article, we read about the basics of Python. Now, we continue with some more python concepts. Strings in Python: A string is a sequence of characters that can be a combination of letters, numbers, and special characters. It can be declared in python by using single quotes, double quo
3 min read
Convert String to Int Python
In Python, converting a string to an integer is important for performing mathematical operations, processing user input and efficiently handling data. This article will explore different ways to perform this conversion, including error handling and other method to validate input string during conver
3 min read