按固定大小列表列表中的一个元素排序并删除双打

Sort by one element in list of lists of fixed size and remove doubles

我想找到实现此目标的最佳方法。 (我已经可以手动完成,但我正在寻找可以直接执行此操作的图书馆)。

我想根据第一个索引对列表列表进行排序并删除第一个索引的双精度值。

sections = [
    [1, 4, 1],
    [5, 3, 2],  # Sort on the first element of sublist
    [2, 2, 3],
    [2, 1, 4],  # Double to remove
]

assertion = [
    [1, 4, 1],
    [2, 2, 3],
    [5, 3, 2],
]

def sort_first_and_remove_double(sections):
    
    return result


assert sort_first_and_remove_double(sections) == assertion

目标是不直接编写任何循环,而是使用库在一两行中编写,出于优化原因,我的数据量很大。

我的尝试代码在这里:

def sort_first_and_remove_double(sections):
    sections = remove_double(sections)
    sections = sort_sections(sections)
    return sections


def sort_sections(sections):
    ids = get_ids(sections)
    return [x for _, x in sorted(zip(ids, sections))]


def remove_double(sections):
    ids = get_ids(sections)
    keeped_sections = []
    for i, id_check in enumerate(ids):
        ids_keeped = get_ids(keeped_sections)
        if id_check not in ids_keeped:
            keeped_sections.append(sections[i])
    return keeped_sections


def get_ids(sections):
    return [section[0] for section in sections]

但是很慢!

编辑:这里更新时间:

t0 = time.perf_counter()
sort_first_and_remove_double(lst)
print(time.perf_counter() - t0)  # 7.719699351582676e-05 s
t0 = time.perf_counter()
list(dict(map(lambda x: [x[1][0], x[1]], sorted(enumerate(lst), key=lambda pair: (pair[1][0], -pair[0])))).values())
print(time.perf_counter() - t0)  # 7.587004802189767e-06 s
t0 = time.perf_counter()
sorted(dict(map(lambda x: (x[0], x), lst[::-1])).values())
print(time.perf_counter() - t0)  # 3.0700030038133264e-06 s

你不会这样解决吗?

sorted({x[0]: x for x in sections[::-1]}.values())

或没有 for

sorted(dict(map(lambda x: (x[0], x), sections[::-1])).values())