简体   繁体   中英

sorting of list of list by considering two or three index (combining ascending and descending order) in python

I think it is a challenging one...

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"]]

i have given the priority to bad as 1, average as 2, good as 3 and excellent as 4. i have got the output of ascending order sorting considering 1,2 or 3 index by a default functin.

considering 1 index -- 1st level sorting

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

output==>

 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']

considering 2 index -- 2nd level sorting:

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

output==>

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

considering 3 index -- 3rd level sorting

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

output==>

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

These sorting are in ascending order.

I WANT TO GET SOME IN ASCENDING AND SOME IN DESCENDING ORDER.

like, if i want to get the out put in which 3rd index in descending order, 2nd index in ascending order and 1st index in descending order.

WHAT SHOULD I NEED TO DO???

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

You can combine your sorting into one operation, or four independent ones. First though I made a dictionary so I could look up the rating

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

Next, sort the list. You can either do it one at a time:

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

Where the last sort takes precedent and the previous sorts will only take effect when the last one was equal

Sorting all at once gets complicated quickly, but should sort faster:

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])
              ])
         )

Where the first of the four items in the list takes precedent, and negative signs sort that element backwards

I will extract two key points from your problem statement

  • i have given the priority to bad as 1, average as 2, good as 3 and excellent as 4

  • i want to get the out put in which 3rd index in descending order, 2nd index in ascending order and 1st index in descending order.

Implementation

#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))

Output

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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