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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14802128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:28:49  来源:igfitidea点击:

Tuple pairs, finding minimum using python

pythontuplesmin

提问by Harry Lime

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.

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

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?

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

i.e.

IE

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

min( data ) = (5, 0.01)

分钟(数据)= (5, 0.01)

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

min( data )回报(1, 7.57),这是我接受的是为最小索引0的是正确的,但我想最少指数1。

采纳答案by Lev Levitsky

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

or:

或者:

In [3]: import operator

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

回答by user1767754

Even though Lev's answer is correct, I wanted to add the sort Method as well, in case someone is interested in the first nminimas. One thing to consider is that the minoperation's runtime is O(N)where the sort's is O(N Log N)

即使 Lev 的答案是正确的,我也想添加 sort 方法,以防有人对第一个n最小值感兴趣。需要考虑的一件事是min操作的运行时是O(N)排序所在的位置O(N Log N)

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)]
data.sort(key=lambda x:x[1])
print data

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

https://www.ics.uci.edu/~pattis/ICS-33/lectures/complexitypython.txt

https://www.ics.uci.edu/~pattis/ICS-33/lectures/complexitypython.txt

回答by Eric Leschinski

If you're willing to drink the numpy coolaid, you can use these commands to get the tuple in list where item is minimum:

如果你愿意喝 numpy coolaid,你可以使用这些命令来获取列表中 item 最小的元组:

The ingredients that make this work are numpy's advanced array slicing and argsort features.

使这项工作发挥作用的成分是 numpy 的高级数组切片和 argsort 功能。

import numpy as np
#create a python list of tuples and convert it to a numpy ndarray of floats
data = np.array([ (1, 7.57), (2, 2.1), (3, 1.2), 
                  (4, 2.1), (5, 0.01), (6, 0.5), (7, 0.2), (8, 0.6)])

print("data is")
print(data)

#Generate sortIndices from second column
sortIndices = np.argsort(data[:,1])

print("sortIndices using index 1 is:" )
print(sortIndices)
print("The column at index 1 is:")
print(data[:,1])
print("Index 1 put into order using column 1")
print(data[sortIndices,1])
print("The tuples put into order using column 1")
print(data[sortIndices,:])
print("The tuple with minimum value at index 1")
print(data[sortIndices[0],:])
print("The tuple with maximum value at index 1")
print(data[sortIndices[-1],:])

Which prints:

哪个打印:

data is
[[ 1.    7.57]
 [ 2.    2.1 ]
 [ 3.    1.2 ]
 [ 4.    2.1 ]
 [ 5.    0.01]
 [ 6.    0.5 ]
 [ 7.    0.2 ]
 [ 8.    0.6 ]]

sortIndices using index 1 is:
[4 6 5 7 2 1 3 0]

The column at index 1 is:
[ 7.57  2.1   1.2   2.1   0.01  0.5   0.2   0.6 ]

Index 1 put into order using column 1
[ 0.01  0.2   0.5   0.6   1.2   2.1   2.1   7.57]

The tuples put into order using column 1
[[ 5.    0.01]
 [ 7.    0.2 ]
 [ 6.    0.5 ]
 [ 8.    0.6 ]
 [ 3.    1.2 ]
 [ 2.    2.1 ]
 [ 4.    2.1 ]
 [ 1.    7.57]]

The tuple with minimum value at index 1
[ 5.    0.01]

The tuple with maximum value at index 1
[ 1.    7.57]