元组对,使用 python 找到最小值

2023-08-31Python开发问题
2

本文介绍了元组对,使用 python 找到最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想找到按给定列排序的元组列表的最小值.例如,我将一些数据排列为 2 元组列表.

I want to find the minimum of a list of tuples sorting by a given column. I have some data arranged as a list of 2-tuples for example.

data = [ (1, 7.57), (2, 2.1), (3, 1.2), (4, 2.1), (5, 0.01), 
         (6, 0.5), (7, 0.2), (8, 0.6)]

如何仅通过比较元组中的第二个数字来找到数据集的最小值?

How may I find the min of the dataset by the comparison of the second number in the tuples only?

data[0][1] = 7.57
data[1][1] = 2.1

min( 数据 ) = (5, 0.01)

min( data ) = (5, 0.01)

min(data) 返回 (1, 7.57),我接受它对于索引 0 的最小值是正确的,但我想要索引 1 的最小值.

min( data ) returns (1, 7.57), which I accept is correct for the minimum of index 0, but I want minimum of index 1.

推荐答案

In [2]: min(data, key = lambda t: t[1])
Out[2]: (5, 0.01)

或:

In [3]: import operator

In [4]: min(data, key=operator.itemgetter(1))
Out[4]: (5, 0.01)

这篇关于元组对,使用 python 找到最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

pandas 有从特定日期开始的按月分组的方式吗?
Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)...
2024-08-22 Python开发问题
10

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10