繁体   English   中英

从python中嵌套列表的索引中查找下一个最大值?

[英]Finding next max value from index of nested lists in python?

我正在尝试查找嵌套列表的下一个最大值,我已经有一个按冒泡排序排序的嵌套列表,我需要将每个嵌套列表的最大元素插入到解向量中,直到对解向量进行排序。 PS:我无法从初始嵌套列表中删除元素,只能找到下一个最大值。 以底部的图像为例:

Nested_list = [[1, 7, 9], [4, 5, 6], [2, 3, 8], [0]]

我设计的方式从原始列表中删除了最大的向量,这非常耗时,我相信只需将索引移动到下一个最大值将消耗更少的时间:

def bubbleSort(array):
    n = len(array)-1
    for i in range(n):
        for j in range(0, n-i):
            if array[j] > array[j+1]:
                array[j], array[j+1] = array[j+1], array[j]
            else:
                continue
    return array

def ordena_lista(output): 
    for sublista in output:
        bubbleSort(sublista)
       
        
def maior_valor_lista(output):
    return list(el[-1] for el in output)


def nested_remove(L, x):
    if x in L:
        L.remove(x)
    else:
        for element in L:
            if type(element) is list:
                nested_remove(element, x)

b = list(random.sample(range(10), 10))
n= m.floor(m.sqrt(len(b)))
output=list([b[i:i + n] for i in range(0, len(b), n)])
ordena_lista(b)
while output:
        valores_maximo = maior_valor_lista(output)
        var = max(valores_maximo, key=int)
        final = [var] + final
        nested_remove(output, var)
        output = list(filter(None, output))

https://i.stack.imgur.com/AISNc.png

这相当于一个奇怪的、可悲的低效且完全不必要的排序算法,但无论如何:

Nested_list = [[9, 7, 1], [4, 5, 6], [2, 3, 8], [0]]

for e in Nested_list:
    e.sort()

Output_list = []

Nested_list_copy = [[e_ for e_ in e] for e in Nested_list]

element_count = sum(len(e) for e in Nested_list)

for _ in range(element_count):
    m = None
    for i, e in enumerate(Nested_list_copy):
        if e:
            tm = e[-1]
            if m is None or tm > m:
                m = tm
                k = i
    Output_list.insert(0, Nested_list_copy[k].pop())

print(Nested_list)
print(Output_list)

输出:

[[1, 7, 9], [4, 5, 6], [2, 3, 8], [0]]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

最简单的解决方案如下,

from functools import reduce
from operator import add


def sort_nested_list(nested_list):
   return sorted(reduce(add, nested_list))

但是,在不知道 python 排序的确切实现细节的情况下,我无法告诉你它是否利用了你的预排序。

如果我们知道子列表已排序,并且我们可以复制列表,并且我们知道总共有多少元素,我们可以编写以下内容,

import math
from copy import deepcopy


def get_max_and_pop(nested_list):
    """ find the maximum element of a sublist of nested_list, remove it from the sublist, and return it """
    print(f"get_max_and_pop says: {nested_list}")
    return max(nested_list, key=lambda x: x[-1:]).pop()


def sort_nested_list_whose_sublists_are_sorted(nested_list, n_elements):
    nested_list_copy = deepcopy(nested_list)
    return [get_max_and_pop(nested_list=nested_list_copy) for _ in range(n_elements)][::-1]

编辑:不知道元素的数量,我们可以写,

from copy import deepcopy


def sort_nested_list_whose_sublists_are_sorted_iter(nested_list):
    nested_list_copy = deepcopy(nested_list)
    while any(nested_list_copy):
        yield max(nested_list_copy, key=lambda x: x[-1:]).pop()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM