目录
Find Min and Max values in a list of tuples in Python
在 Python 中查找元组列表中的最小值和最大值
要在元组列表中查找最小值和最大值:
- 使用
min()
和max()
功能。 - 将
key
参数传递给函数。 - 选择要比较的元组中的元素。
my_list = [(100, 1), (100, 2), (100, 3)] # ✅ get the Min tuple in list of tuples min_tuple = min(my_list, key=lambda tup: tup[1]) print(min_tuple) # 👉️ (100, 1) # ------------------------------------------------------ # ✅ get the Max tuple in a list of tuples max_tuple = max(my_list, key=lambda tup: tup[1]) print(max_tuple) # 👉️ (100, 3)
min函数返回可迭代对象中的最小项或两个或多个参数中最小的一个。
max函数返回可迭代对象中的最大项或两个或多个参数中最大的一个。
这些函数采用可选的default
关键字参数,用于指定在提供的可迭代对象为空时要返回的值。
result = max((), default=0) print(result) # 👉️ 0
如果可迭代对象为空default
且未提供关键字参数,则函数会引发一个ValueError
.
key
参数指定一个单参数排序函数,如用于 的函数list.sort()
。my_list = [(100, 1), (100, 2), (100, 3)] min_tuple = min(my_list, key=lambda tup: tup[1]) print(min_tuple) # 👉️ (100, 1)
我们选择索引处的元组项1
来比较元组中的第二项。
根据不同的条件获取最小值和最大值
您还可以使用key
参数根据不同的条件获取元组列表中的最小值和最大值。
my_list = [(100, 'a'), (100, 'ab'), (100, 'abc')] # ✅ get value with min length in the list of tuples min_tuple = min(my_list, key=lambda tup: len(tup[1])) print(min_tuple) # 👉️ (100, 'a') # ------------------------------------------------------ # ✅ get value with max length in the list of tuples max_tuple = max(my_list, key=lambda tup: len(tup[1])) print(max_tuple) # 👉️ (100, 'abc')
我们将每个元组中的第二项传递给函数len()
以获取字符串的长度。
您为参数提供的函数key
可用于根据不同的条件获取min
和
值。max
使用 itemgetter 在元组列表中查找最小值和最大值
您还可以使用该类operator.itemgetter
在元组列表中查找最小值和最大值。
from operator import itemgetter my_list = [(100, 1), (100, 2), (100, 3)] # ✅ get the Min tuple in list of tuples min_tuple = min(my_list, key=itemgetter(1)) print(min_tuple) # 👉️ (100, 1) # ------------------------------------------------------ # ✅ get the Max tuple in a list of tuples max_tuple = max(my_list, key=itemgetter(1)) print(max_tuple) # 👉️ (100, 3)
代码示例使用operator.itemgetter
而不是使用 lambda 函数。
operator.itemgetter
类返回一个可调用对象,该对象获取指定索引处
的项目。
例如,x = itemgetter(1)
然后调用x(my_tuple)
,返回
my_tuple[1]
。
在 Python 中获取元组中的最大值
使用max()
函数获取元组中的最大值。
该max()
函数返回可迭代对象(例如元组)中的最小项。
my_tuple = (1, 3, 5, 7, 9) # ✅ get the max value in a tuple max_value = max(my_tuple) print(max_value) # 👉️ 9 # ----------------------------------------- # ✅ get the item with max length in a tuple my_tuple = ('a', 'ab', 'abc') max_length = max(my_tuple, key=lambda val: len(val)) print(max_length) # 👉️ 'abc'
我们使用该max()
函数来获取元组中的最大值。
max函数返回可迭代对象中的最大项或两个或多个参数中最大的一个。
my_tuple = (15, 45, 30) result = max(my_tuple) print(result) # 👉️ 45
也可以使用两个或多个位置参数调用该函数。
result = max(15, 45, 30) print(result) # 👉️ 45
该函数采用可选的default
关键字参数,用于指定在提供的可迭代对象为空时返回的值。
result = max((), default=0) print(result) # 👉️ 0
如果可迭代对象为空default
且未提供关键字参数,则该函数会引发一个ValueError
.
如果需要获取最大长度的元组元素,请使用key
关键字参数。
my_tuple = ('a', 'ab', 'abc') max_length = max(my_tuple, key=lambda val: len(val)) print(max_length) # 👉️ 'abc'
The key
argument specifies a one-argument ordering function like the one used
for list.sort()
.
We set the argument to a lambda
function that passes each tuple element to the
len()
function and returns the result.
THe max()
function returns the tuple element with max
length.
# Get the Min value in a Tuple in Python
Use the min()
function to get the min value in a tuple.
The min()
function returns the smallest item in an iterable (such as a tuple).
my_tuple = (1, 3, 5, 7, 9) # ✅ get min value in a tuple min_value = min(my_tuple) print(min_value) # 👉️ 1 # ----------------------------------------- # ✅ get the item with min length in a tuple my_tuple = ('a', 'ab', 'abc') min_length = min(my_tuple, key=lambda val: len(val)) print(min_length) # 👉️ 'a'
We used the min()
function to get the min value in a tuple.
The min function returns
the smallest item in an iterable or the smallest of two or more arguments.
my_tuple = (10, 5, 20) result = min(my_tuple) print(result) # 👉️ 5
The function can also be called with or more positional arguments.
result = min(10, 5, 20) print(result) # 👉️ 5
The function takes an optional default
keyword argument which is used to
specify a value to return if the provided iterable is empty.
result = min((), default=0) print(result) # 👉️ 0
If the iterable is empty and the default
keyword argument is not provided, the
function raises a ValueError
.
You can use the key
argument of the min()
function to get the min value in a
tuple according to different criteria.
Here is an example that finds the value with the min length in a tuple.
my_tuple = ('a', 'ab', 'abc') min_length = min(my_tuple, key=lambda val: len(val)) print(min_length) # 👉️ 'a'
The lambda function gets called with each element of the tuple and returns its
length.
The min()
function is then called with the length of the elements and returns
the min
length in the tuple.
# Additional Resources
You can learn more about the related topics by checking out the following
tutorials: