按层次顺序降序百分比对元组列表进行排序

时间:2017-08-24 21:19:36

标签: python list sorting

我试图按优势顺序(以百分比递减)对多个级别的成分声明进行排序。

我使用Python并且我有一个元组列表,每个元组都有以下变量:(成分,百分比,childID,parentID)。

它来自看起来像这样的数据,可以按任何顺序输入数据。下面的列是成分/子成分,百分比,childID,parentID。

#Ing1   30%             1   0
#---Sub1    30%         2   1
#---Sub2    60%         3   1
#------Sub3     15%     4   3
#------Sub4     85%     5   3
#---Sub5    10%         6   1
#Ing2   10%             7   0
#Ing3   60%             5   0

我现有的代码会在这样的列表中输出这个(输入的顺序):

list = [(Ing1,30,1,0),(Sub1,30,2,1),(Sub2,60,3,1),(Sub3,15,4,3),(Sub4,85,5,3),(Sub5,10,6,1),(Ing2,10,7,0),(Ing3,60,5,0)]

我需要做的是将此列表按降序购买百分比排序,同时保持层次结构从较低级别保持不变。所以3级成分(Sub3,Sub4)首先,然后是下一级,然后是顶级 子级别需要与父级进行排序。

因此,对于上面的示例,我需要输出按此顺序:

> #Ing3 60%             5   0
> #Ing1 30%             1   0
> #---Sub2  60%         3   1
> #------Sub4   85%     5   3
> #------Sub3   15%     4   3
> #---Sub1  30%         2   1
> #---Sub5  10%         6   1
> #Ing2 10%             7   0

所以列表应如下所示:

list = [(Ing3,60,5,0),(Ing1,30,1,0),(Sub2,60,3,1),(Sub4,85,5,3),(Sub3,15,4,3),(Sub1,30,2,1),(Sub5,10,6,1),(Ing2,10,7,0)]

在Python中,最优雅的方法是什么?哦,另一个警告,因为我限制了我可以导入的模块。如果它不是附带的模块,由于我的环境,我可能无法访问它。

1 个答案:

答案 0 :(得分:2)

您可以使用这样的生成器:

lst = [('Ing1',30,1,0),
       ('Sub1',30,2,1),
       ('Sub2',60,3,1),
       ('Sub3',15,4,3),
       ('Sub4',85,5,3),
       ('Sub5',10,6,1),
       ('Ing2',10,7,0),
       ('Ing3',60,5,0)]

def sort_hierarchical(lst, parent=0):
    # sort the current layer (excluding all other elements) by the second element
    res = sorted([i for i in lst if i[3] == parent], key=lambda x: x[1], reverse=True)
    for item in res:
        yield item
        # recurse for all childs of this item
        for subitem in sort_hierarchical(lst, parent=item[2]):
            yield subitem

>>> list(sort_hierarchical(lst))
[('Ing3', 60, 5, 0),
 ('Ing1', 30, 1, 0),
 ('Sub2', 60, 3, 1),
 ('Sub4', 85, 5, 3),
 ('Sub3', 15, 4, 3),
 ('Sub1', 30, 2, 1),
 ('Sub5', 10, 6, 1),
 ('Ing2', 10, 7, 0)]

如果在将列表传递给函数之前对列表进行一次排序,它甚至可以进一步简化。然后,您只需要过滤多次不对它们进行排序的项目:

def return_hierarchical(lst, parent=0):
    for item in (i for i in lst if i[3] == parent):
        yield item
        for subitem in return_hierarchical(lst, parent=item[2]):
            yield subitem

>>> list(return_hierarchical(sorted(lst, key=lambda x: x[1], reverse=True)))
[('Ing3', 60, 5, 0),
 ('Ing1', 30, 1, 0),
 ('Sub2', 60, 3, 1),
 ('Sub4', 85, 5, 3),
 ('Sub3', 15, 4, 3),
 ('Sub1', 30, 2, 1),
 ('Sub5', 10, 6, 1),
 ('Ing2', 10, 7, 0)]

在Python-3.3 +中,您可以使用yield from并使其更短:

def return_hierarchical(lst, parent=0):
    for item in (i for i in lst if i[3] == parent):
        yield item
        yield from return_hierarchical(lst, parent=item[2])

一般说明:

我将您的list重命名为lst,因此不会影响内置list

您正在处理元组,但是您可以通过名称来引用元组,因此您也可以使用collections.namedtuple。这允许您也可以按属性引用项目:

from collections import namedtuple

ingredient = namedtuple('Ingredient', ['ingredient', 'percentage', 'order', 'parent'])

lst = [ingredient('Ing1',30,1,0), ingredient('Sub1',30,2,1), ingredient('Sub2',60,3,1),
       ingredient('Sub3',15,4,3), ingredient('Sub4',85,5,3), ingredient('Sub5',10,6,1),
       ingredient('Ing2',10,7,0), ingredient('Ing3',60,5,0)]

def return_hierarchical(lst, parent=0):
    for item in (i for i in lst if i.parent == parent):
        yield item
        yield from return_hierarchical(lst, parent=item.parent)

list(sort_hierarchical(sorted(lst, key=lambda x: x.percentage, reverse=True)))

就我个人而言,我喜欢namedtuple,但有些人不喜欢,你说你受到了进口的限制(虽然它在标准库中但是仍然如此)所以我只把它包含在这里......最后。< / p>