Saturday, November 16, 2024
Google search engine
HomeLanguagesPython | Min/Max value in float string list

Python | Min/Max value in float string list

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




# Python3 code to demonstrate working of
# Min / Max value in float string list
# using min()/max() + float() + generator
 
# initialize lists
test_list = ['4.5', '7.8', '9.8', '10.3']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Min / Max value in float string list
# using min()/max() +float + lambda function
res_min = min(test_list,key=lambda x:float(x))
res_max = max(test_list,key=lambda x:float(x))
 
# printing result
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']
 
# Find minimum value
res_min = reduce(lambda x, y: x if float(x) < float(y) else y, test_list)
 
# Find maximum value
res_max = reduce(lambda x, y: x if float(x) > float(y) else y, test_list)
 
# printing result
print("The min value of list : " + str(res_min))
print("The max value of list : " + str(res_max))
#This code is contributed by Edula Vinay Kumar Reddy


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




# Python3 code to demonstrate working of
# Min / Max value in float string list
# using for loop and float()
 
# initialize list
test_list = ['4.5', '7.8', '9.8', '10.3']
 
# printing original list
print("The original list is : " + str(test_list))
 
# initialize variables for minimum and maximum values
min_val = None
max_val = None
 
# iterate through the list and find minimum and maximum values
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
 
# printing result
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 :

  1. Import the numpy library using the statement import numpy as np.
  2. Define the input list test_list with string elements as [‘4.5’, ‘7.8’, ‘9.8’, ‘10.3’].
  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.
  4. Use the np.min() function to get the minimum value from the float_arr array and assign it to a variable min_val.
  5. Use the np.max() function to get the maximum value from the float_arr array and assign it to a variable max_val.
  6. 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




# Python3 code to demonstrate working of
# Min / Max value in float string list
# using numpy
 
import numpy as np
 
# initialize list
test_list = ['4.5', '7.8', '9.8', '10.3']
 
# convert the list of string elements to a numpy array of float elements
float_arr = np.array(test_list, dtype=float)
 
# get the minimum and maximum values from the array
min_val = np.min(float_arr)
max_val = np.max(float_arr)
 
# printing result
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).

RELATED ARTICLES

Most Popular

Recent Comments