通过在python中考虑两个或三个索引(组合升序和降序)来排序列表列表

时间:2014-01-17 07:30:07

标签: python list sorting

我认为这是一个具有挑战性的......

list = [["hasan",6,"bad","chennai"], 
        ["vishnu",7,"good","chennai"], 
        ["tabraiz",8,"good","bangalore"], 
        ["shaik",5,"excellent","chennai"], 
        ["mani",6,"avarage","kerala"], 
        ["cilvin",9,"excellent","chennai"]]

我将优先级设为1,平均为2,优先为3,优秀为4。 我有一个升序排序的输出,考虑1,2或3索引的默认功能。

考虑1个指数 - 第1级排序

list.sort(key= lambda x: x[3])

输出==>

 for i in list:
     print i

['tabraiz', 8, 'good', 'bangalore']
['hasan', 6, 'bad', 'chennai']
['vishnu', 7, 'good', 'chennai']
['shaik', 5, 'excellent', 'chennai']
['cilvin', 9, 'excellent', 'chennai']
['mani', 6, 'avarage', 'kerala']

考虑2指数 - 第二级排序:

list.sort(key= lambda x: (x[3],x[2]) 

输出==>

['tabraiz', 8, 'good', 'bangalore']
['hasan', 6, 'bad', 'chennai']
['vishnu', 7, 'good', 'chennai']
['shaik', 5, 'excellent', 'chennai']
['cilvin', 9, 'excellent', 'chennai']
['mani', 6, 'avarage', 'kerala']

考虑3指数 - 第3级排序

list.sort(key= lambda x: (x[3],x[2],x[1]) 

输出==>

['tabraiz', 8, 'good', 'bangalore']
['hasan', 6, 'bad', 'chennai']
['vishnu', 7, 'good', 'chennai']
['shaik', 5, 'excellent', 'chennai']
['cilvin', 9, 'excellent', 'chennai']
['mani', 6, 'avarage', 'kerala']

这些排序按升序排列。

我想要进行一些升级和下降的命令。

喜欢,如果我想以降序排列第3个索引,第2个索引按升序排列,第1个索引按降序排列。

我应该做什么?

['mani', 6, 'avarage', 'kerala']
['hasan', 6, 'bad', 'chennai']
['vishnu', 7, 'good', 'chennai']
['cilvin', 9, 'excellent', 'chennai']
['shaik', 5, 'excellent', 'chennai']
['tabraiz', 8, 'good', 'bangalore']

2 个答案:

答案 0 :(得分:0)

您可以将排序合并为一个操作,也可以组合为四个独立操作。首先,虽然我做了一本字典,所以我可以查看评级

rating = {'excellent':4, 'good':3, 'average':2, 'bad':1}

接下来,对列表进行排序。你可以一次做一个:

list.sort(key=lambda x:x[0], reverse=False)         # sorting first name alphabetically
list.sort(key=lambda x:x[1], reverse=True)         # sorting by whatever the number is
list.sort(key=lambda x:rating[x[2]], reverse=True) # sorting by rating, with lookup
list.sort(key=lambda x:x[3], reverse=False)         # sorting by last name

最后一个排序采用先例,之前的排序仅在最后一个排序等于

时生效

一次排序会很快变得复杂,但应该更快排序:

list.sort(cmp=lambda x,y:
              next([-cmp(x[0], y[0]),
                    cmp(x[1], y[1]),
                   -cmp(rating[x[2]], rating[y[2]]),
                    cmp(x[3], y[3])
              ])
         )

如果列表中的四个项目中的第一个先于先例,而负号则将该元素向后排序

答案 1 :(得分:0)

我将从你的问题陈述中提取两个要点

  • 我将优先级设为1,平均为2,优先为3,优秀为4

  • 我希望得出第3个索引按降序排列,第2个索引按升序排列,第1个索引按降序排列。

<强>实施

#Remember do not name a variable that masks out a Python built-in
lst = [["hasan",6,"bad","chennai"],
        ["vishnu",7,"good","chennai"],
        ["tabraiz",8,"good","bangalore"],
        ["shaik",5,"excellent","chennai"],
        ["mani",6,"average","kerala"],
        ["cilvin",9,"excellent","chennai"]]
#i have given the priority to bad as 1, average as 2, good as 3 
#and excellent as 4
priority = {"bad":1, "average":2, "good":3, "excellent":4}
#3rd index in descending order, 2nd index in ascending order 
#and 1st index in descending order
def key(elem):
    return priority[elem[2]], -elem[1], elem[0]
pprint.pprint(sorted(lst, key = key, reverse = True))

<强>输出

[['shaik', 5, 'excellent', 'chennai'],
 ['cilvin', 9, 'excellent', 'chennai'],
 ['vishnu', 7, 'good', 'chennai'],
 ['tabraiz', 8, 'good', 'bangalore'],
 ['mani', 6, 'average', 'kerala'],
 ['hasan', 6, 'bad', 'chennai']]