Python在2D列表的第一列中查找重复项,并基于第二列删除其中一个

Python在2D列表的第一列中查找重复项,并基于第二列删除其中一个,python,list,Python,List,我正在寻找一种从2D列表中删除重复项的方法,但基于第二列中的最高值。我知道如何在几个for循环中实现,但我正在寻找一个O(N)解决方案。 样本输入: inputLst = [[100,150] ,[150,140] ,[200,180] ,[300,150] ,[300,100] ,[320,180]] 输出应为: OutputLst = [[100,150] ,[150,

我正在寻找一种从2D列表中删除重复项的方法,但基于第二列中的最高值。我知道如何在几个for循环中实现,但我正在寻找一个O(N)解决方案。 样本输入:

inputLst = [[100,150]
          ,[150,140]
          ,[200,180]
          ,[300,150]
          ,[300,100]
          ,[320,180]]
输出应为:

OutputLst = [[100,150]
           ,[150,140]
           ,[200,180]
           ,[300,150]
           ,[320,180]]
这是我现在拥有的代码:

        SortLst = [[100,150],[150,140],[200,180],[300,150],[300,100],[320,180]]
        lst = []
        lastRow = SortLst[0]
        for row in SortLst+[0,0]:
            if row[0] != lastRow[0]:
                lst.append(lastRow)
                lastRow = row
            elif row [1] > lastRow[1]:
                lastRow =  row
Sortlst按以下顺序排序:

SortLst = sorted(zip(self.WavelengthPanel.GetValues(col=1),self.Shuttertime))

你可以看看这个

from pprint import pprint

input_lst = [[100, 150],
             [150, 140],
             [200, 180],
             [300, 150],
             [300, 100],
             [320, 180]]

output_lst = dict()

for n, v in input_lst:
    if n in output_lst:    # O(1)
        output_lst[n] = max(output_lst[n], v)    # O(1)
    else:
        output_lst[n] = v    # The question is, what is the complexity of this operation 

pprint(output_lst, width=10)
输出

{100: 150,
 150: 140,
 200: 180,
 300: 150,
 320: 180}

您可以轻松地将输出转换为列表

你可以看看这个

from pprint import pprint

input_lst = [[100, 150],
             [150, 140],
             [200, 180],
             [300, 150],
             [300, 100],
             [320, 180]]

output_lst = dict()

for n, v in input_lst:
    if n in output_lst:    # O(1)
        output_lst[n] = max(output_lst[n], v)    # O(1)
    else:
        output_lst[n] = v    # The question is, what is the complexity of this operation 

pprint(output_lst, width=10)
输出

{100: 150,
 150: 140,
 200: 180,
 300: 150,
 320: 180}

您可以轻松地将输出转换为列表

查看
itertools.groupby()
(),然后当输入为
[[100110],[100120]]
时,输出应为
[[100120]]
?是的,那么输出应为[[100120]]好的,重要的是要知道,
inputLst
已经排序了?您知道排序总是O(N log N)。查看
itertools.groupby()
()那么当输入是
[[100110],[100120]]
时,输出应该是
[[100120]]
?是的,那么输出应该是[[100120]]好的,重要的是要知道,
inputLst
已经排序了?您知道排序总是O(N log N)。