


Find the minimum and maximum value in a list of tuples using Python
introduce
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.
Find the minimum and maximum values in a list of 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
Method 1: Using numpy library
Method 2: Using min and max functions
Method 3: Using lambda function
Method 1: Python program to find the maximum and minimum value in a list of tuples using the Numpy module
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.
algorithm
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 ofExample
is:Example
#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)
Output
Minimum value in the input: [1 1] Maximum value in the input: [4 4]
Method 2: Python program to find minimum and maximum values in a list of tuples using functions
algorithm
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 ofExample
is:Example
#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)
Output
Minimum value in the input: (1, 1) Maximum value in the input: (4, 4)
Method 3: Python program to find the maximum and minimum values in a list of tuples using Lambda function
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.
algorithm
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 ofExample
is:Example
#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)
Output
Minimum value in the input: (1,1) Maximum value in the input: (4,4)
in conclusion
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

Fastapi ...

Using python in Linux terminal...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

About Pythonasyncio...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...

Loading pickle file in Python 3.6 environment error: ModuleNotFoundError:Nomodulenamed...

Discussion on the reasons why pipeline files cannot be written when using Scapy crawlers When learning and using Scapy crawlers for persistent data storage, you may encounter pipeline files...
