Table of Contents
introduce
Find the minimum and maximum values ​​in a list of tuples
method
Method 1: Python program to find the maximum and minimum value in a list of tuples using the Numpy module
algorithm
Example
Output
Method 2: Python program to find minimum and maximum values ​​in a list of tuples using functions
Method 3: Python program to find the maximum and minimum values ​​in a list of tuples using Lambda function
in conclusion
Home Backend Development Python Tutorial Find the minimum and maximum value in a list of tuples using Python

Find the minimum and maximum value in a list of tuples using Python

Sep 19, 2023 pm 05:49 PM

使用 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 of

Example

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)
Copy after login

Output

Minimum value in the input: [1 1]
Maximum value in the input: [4 4]
Copy after login

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 of

Example

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)
Copy after login

Output

Minimum value in the input: (1, 1)
Maximum value in the input: (4, 4)
Copy after login

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 of

Example

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)
Copy after login

Output

Minimum value in the input: (1,1)
Maximum value in the input: (4,4)
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

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

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

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...

How to get news data bypassing Investing.com's anti-crawler mechanism? How to get news data bypassing Investing.com's anti-crawler mechanism? Apr 02, 2025 am 07:03 AM

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)...

Python 3.6 loading pickle file error ModuleNotFoundError: What should I do if I load pickle file '__builtin__'? Python 3.6 loading pickle file error ModuleNotFoundError: What should I do if I load pickle file '__builtin__'? Apr 02, 2025 am 06:27 AM

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

What is the reason why pipeline files cannot be written when using Scapy crawler? What is the reason why pipeline files cannot be written when using Scapy crawler? Apr 02, 2025 am 06:45 AM

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...

See all articles