查找第二个元素最高的列表(Python)
我有一个由列表(元组?)组成的列表,它们如下表所示查找第二个元素最高的列表(Python),python,list,element,Python,List,Element,我有一个由列表(元组?)组成的列表,它们如下表所示 [(2,1),(3,0),(4,3),(5,2),(9,2),(7,4)] 我需要确定满足所列标准的元素: 具有最高的第二(?)值。例如,在上面的示例中,输出将是7 若系结,则为第一(?)值最低的元件。例如: [(5,1),(4,2),(1,2),(9,3),(8,3)] 这将返回8;9和8都有最高的第二(?)值,因此在平局中,8比9低,所以8胜 *我把我的术语放在哪里可能是错误的,但希望我的文章可读性好只需按第二个元素排序,然后按第一个元
[(2,1),(3,0),(4,3),(5,2),(9,2),(7,4)]
我需要确定满足所列标准的元素:
[(5,1),(4,2),(1,2),(9,3),(8,3)]
这将返回8;9和8都有最高的第二(?)值,因此在平局中,8比9低,所以8胜>>> lst=[(8,4),(2,1),(3,0),(4,3),(5,2),(9,2),(7,4)]
>>> sorted(lst, key=lambda x: (x[1], -x[0]))[-1]
(7, 4)
再想一想,您不需要对整个列表进行排序就可以只找到一个元素。使用相同的按键功能:
>>> lst=[(8,4),(2,1),(3,0),(4,3),(5,2),(9,2),(7,4)]
>>> max(lst, key=lambda x: (x[1], -x[0]))
(7, 4)
只需按第二个元素排序,然后按-第一个元素排序:
>>> lst=[(8,4),(2,1),(3,0),(4,3),(5,2),(9,2),(7,4)]
>>> sorted(lst, key=lambda x: (x[1], -x[0]))[-1]
(7, 4)
再想一想,您不需要对整个列表进行排序就可以只找到一个元素。使用相同的按键功能:
>>> lst=[(8,4),(2,1),(3,0),(4,3),(5,2),(9,2),(7,4)]
>>> max(lst, key=lambda x: (x[1], -x[0]))
(7, 4)
实现您自己的分拣机:
>>> l=[(5,1),(4,2),(1,2),(9,3),(8,3)]
>>> def sorter(t1, t2):
... # if the second elements are equal sort based on the first
... if t1[1] == t2[1]:
... # positive return means higher value
... return t1[0] - t2[0]
... return t2[1] - t1[1]
...
>>> l.sort(sorter) # in place
>>> l
[(8, 3), (9, 3), (1, 2), (4, 2), (5, 1)]
>>> l[0]
(8, 3)
实现您自己的分拣机:
>>> l=[(5,1),(4,2),(1,2),(9,3),(8,3)]
>>> def sorter(t1, t2):
... # if the second elements are equal sort based on the first
... if t1[1] == t2[1]:
... # positive return means higher value
... return t1[0] - t2[0]
... return t2[1] - t1[1]
...
>>> l.sort(sorter) # in place
>>> l
[(8, 3), (9, 3), (1, 2), (4, 2), (5, 1)]
>>> l[0]
(8, 3)
您也可以一次通过列表完成此操作,而无需对其进行排序:
l = [(2,1),(3,0),(4,3),(5,2),(9,2),(7,4)]
def max_second_val(lst):
max = lst[0] #Take first tuple to be the max
for tup in lst: # As you iterate through the tuples...
if tup[1] == max[1]: # If the 2nd elem of the current tuple is equal
if tup[0] < max[0]: # to 2nd elem of curr max, and the first elem of curr
max = tup # tuple is smaller, take this to be the new max
elif tup[1] > max[1]: # Otherwise, if 2nd elem of curr tuple is bigger than
max = tup # curr max, take this to be the new max
return max
l=[(2,1)、(3,0)、(4,3)、(5,2)、(9,2)、(7,4)]
def最大秒值(lst):
max=lst[0]#取第一个元组作为max
对于lst中的tup:#在遍历这些tuple时。。。
如果tup[1]==max[1]:#如果当前tup的第二个元素相等
如果tup[0]max[1]:#否则,如果curr tuple的第二个元素大于
max=tup#curr max,将此作为新的max
返回最大值
您也可以一次通过列表完成此操作,而无需对其进行排序:
l = [(2,1),(3,0),(4,3),(5,2),(9,2),(7,4)]
def max_second_val(lst):
max = lst[0] #Take first tuple to be the max
for tup in lst: # As you iterate through the tuples...
if tup[1] == max[1]: # If the 2nd elem of the current tuple is equal
if tup[0] < max[0]: # to 2nd elem of curr max, and the first elem of curr
max = tup # tuple is smaller, take this to be the new max
elif tup[1] > max[1]: # Otherwise, if 2nd elem of curr tuple is bigger than
max = tup # curr max, take this to be the new max
return max
l=[(2,1)、(3,0)、(4,3)、(5,2)、(9,2)、(7,4)]
def最大秒值(lst):
max=lst[0]#取第一个元组作为max
对于lst中的tup:#在遍历这些tuple时。。。
如果tup[1]==max[1]:#如果当前tup的第二个元素相等
如果tup[0]max[1]:#否则,如果curr tuple的第二个元素大于
max=tup#curr max,将此作为新的max
返回最大值
我尝试了[(5,1)、(4,2)、(1,2)、(9,3)、(8,3)],在排序()中替换lst,它返回了(9,3)应该返回的地方(8,3)@NoobCoder:works here:在codecademy labs编辑器中再次尝试,我一定是在什么地方出错了。为了让我明白你在做什么,key=lambda:(x[1],-x[0])[-1]部分做了什么?@NoobCoder:实际上这可能是我的错,因为我发布的第一个版本有一个bug——你的测试太快了。。)key
是@NoobCoder:x[1]
是第二个值x[0]
是第一个值-x[0]
是第一个被求反的值。将该键函数应用于每个值会得到(4,-8)
,然后是(1,-2)
,依此类推。因此,使用该键函数进行排序将按照第二个值对对对进行排序,并按照您的要求反向返回到第一个值。我在[(5,1),(4,2),(1,2),(9,3),(8,3)]中尝试了这一点,在排序()中替换lst,并返回(9,3)它应该返回(8,3)的位置@NoobCoder:在这里工作:在CodeCodeCode中再试一次我的实验室编辑器,我一定是在什么地方出错了。为了让我明白你在做什么,key=lambda:(x[1],-x[0])[-1]部分做了什么?@NoobCoder:实际上这可能是我的错,因为我发布的第一个版本有一个bug——你的测试太快了。。)key
是@NoobCoder:x[1]
是第二个值x[0]
是第一个值-x[0]
是第一个被求反的值。将该键函数应用于每个值会得到(4,-8)
,然后是(1,-2)
,依此类推。因此,使用该键函数进行排序将为您提供按第二个值排序的对,按照您的要求反向返回到第一个值。好主意,尽管在python中它是max(key=…)
。好主意,尽管在python中它是max(key=…)
。