相关疑难解决方法(0)

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

我想找到按给定列排序的元组列表的最小值.我有一些数据被安排为例如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)]
Run Code Online (Sandbox Code Playgroud)

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

data[0][1] = 7.57
data[1][1] = 2.1
Run Code Online (Sandbox Code Playgroud)

min(数据)= (5, 0.01)

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

python tuples min

50
推荐指数
4
解决办法
4万
查看次数

用最后一个元素对元组列表进行排序

需要使用最后一个元素对给定的元组列表进行排序

def sort_last(tuples):
    sorted_list = []
    i = 0
    i2 = 1
    while True:
        if len(tuples) == 2:
            if tuples[i][-1] < tuples[i2][-1]:
                sorted_list.append(tuples[i])
                sorted_list.append(tuples[i2])
                break
            else:
                sorted_list.append(tuples[i2])
                sorted_list.append(tuples[i])
                break
        elif tuples[i][-1] < tuples[i2][-1]:
            i2 += 1
            print 1
        elif i == i2:
            i2 += 1
        elif i2 == len(tuples)-1:
            if tuples[i][-1] < tuples[i2][-1]:
                sorted_list.append(tuples[i])
                del tuples[i]
                i = 0
                i2 = 1
            else:
                sorted_list.append(tuples[i2])
                del tuples[i2]
                i = 0
                i2 = 1
        elif len(tuples) <= 1:
            return tuples
        else:
            i …
Run Code Online (Sandbox Code Playgroud)

python python-2.7

0
推荐指数
1
解决办法
1213
查看次数

标签 统计

python ×2

min ×1

python-2.7 ×1

tuples ×1