简体   繁体   中英

How to sort ANY list in Python 2.7 in DESCENDING order?

I think the title speaks enough for the question....

This is how i tried to do it:

r=input("rows")
c=input("colomns")
b=[]
for x in range(r):
    b.append(["O"] * c)

for i in range(r):
    for j in range(c):
        b[i][j]=input("enter ")

Now this can be any matrix or a nested list..... But if i use the SORTED fuction then i get the descending order of the lists in the main list.... for example-

list=[[1,2,3],[2,3,4].[4,5,6]]

and if i do sorted(list,reverse=True)

I get [[4,5,6],[2,3,4],[1,2,3] instead of [[6,5,4],[4,3,2],[3,2,1]]

I think we have to loop somewhere.

You need to sort the sublists, too.

>>> lst = [[1,2,3], [2,3,4], [4,5,6]]
>>> sorted([sorted(sublst, reverse=True) for sublst in lst], reverse=True)
[[6, 5, 4], [4, 3, 2], [3, 2, 1]]

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