Home > Article > Backend Development > Find the minimum and maximum value in a list of tuples using Python
The Python language consists of a variety of data structures, and list is the most common one. The elements in the list can be from any data type, such as integer, string, or floating point data types. Elements are represented within square brackets and separated by commas. Using list data structures is the most interesting foundation. Tuple is one of the data structures that can hold elements of different data types. The values that can be saved as tuples are integers, strings, or other tuples.
Tuples can have repeating sequences, and once these elements are assigned, they cannot be changed. It's inside these "()" brackets, separated by commas. It does not support removing elements from tuples.
Method 1: Using numpy library
Method 2: Using min and max functions
Method 3: Using lambda function
Elements are given as input in the list data structure in the form of tuples and the output is presented in the form of tuples using a predefined library called numpy module.
Step 1:The input is given by the tuple elements, the lambda function starts running using the key parameter "a", and the numpy module is imported.
Step 2:Created a list data structure to hold integer values.
Step 3:This function uses the min() method to get the minimum value of the elements in the list.
Step 4: The results are stored in a variable named "minimum_value".
Step 5:Then, the print function will return the list after performing the operation.
The Chinese translation of#importing the library import numpy as np #Creating the list data structure list1 = [(1,1), (3, 3), (2,2), (4,4)] #input list is converted to numpy array num = np.array(list1) #To get the required value min function is declared minimum_list = np.min(num, axis=0) #To get the required value max function is declared maximum_list = np.max(num, axis=0) #print function to get the output print("Minimum value in the input:", minimum_list) print("Maximum value in the input:", maximum_list)
Minimum value in the input: [1 1] Maximum value in the input: [4 4]
Step 1:Input provided.
Step 2: In this case, different functions are defined to perform the operation.
Step 3: Each element is taken into account when calculating the maximum and minimum values using the `max()` and `min()` functions.
Step 4: The ‘max()’ function is used to get the highest value given two list data structures.
Step 5: Finally print the output using the value.
The Chinese translation of#Creating the list data structure list1 = [(1,1), (3, 3), (2,2), (4,4)] #To get the required value min function is declared minimum_list = min(list1) #To get the required value max function is declared maximum_list = max(list1) #print function to get the output print("Minimum value in the input:", minimum_list) print("Maximum value in the input:", maximum_list)
Minimum value in the input: (1, 1) Maximum value in the input: (4, 4)
Sometimes, we may want to specify additional conditions when searching for the maximum or minimum value. Python provides a useful feature where we can apply predicates using lambda functions in these built-in methods.
Step 1: Sometimes we may want to specify additional conditions when searching for the maximum or minimum value. Python provides a useful feature where we can apply predicates using lambda functions in these built-in methods.
Second step: Then pass the resulting filtered set to `min()` to efficiently obtain the required minimum value.
Step 3: The "max()" function is used to get the maximum value of the given double list data structure.
Step 4:The output is returned as a tuple, i.e. within parentheses.
The Chinese translation of#Creating the list data structure list1 = [(1,1), (3, 3), (2,2), (4,4)] num = lambda a: a[1] #To get the required value min function is declared minimum_list = min(list1, key=num) #To get the required value max function is declared maximum_list = max(list1, key=num) #print function to get the output print("Minimum value in the input:", minimum_list) print("Maximum value in the input:", maximum_list)
Minimum value in the input: (1,1) Maximum value in the input: (4,4)
In this article, two data structures are used, namely lists and tuples. The list is initially declared and the tuple data structure is defined within the list. The methods used to find the maximum and minimum values in a list of tuples are min(), max(), lambda functions and finally using the numpy module.
The above is the detailed content of Find the minimum and maximum value in a list of tuples using Python. For more information, please follow other related articles on the PHP Chinese website!