简体   繁体   中英

Python: I need to sort this list in descending order

My code works, for the most part, continuously asking the user for input until they enter a blank line, then sorting said input into a list to be sorted even further. My problem is that I'm meant to sort the list alphabetically first, and then sort it by highest to lowest score. The program for alphabetical order works just fine. But when I tried copying and tweaking it for the sorted score list, it didn't work. Here's the sorted names program:

def sortListName(aList):
    
    sortEm = aList[:]
    
    for x in range(len(sortEm)):
        for y in range(len(sortEm) - 1):
            if (sortEm[y][0].lower() > sortEm[y + 1][0].lower()):
                sortEm[y], sortEm[y + 1] = sortEm[y + 1], sortEm[y]
    return sortEm

Here's the tweaked version for scores:

def sortListScore(aList):
    
    sortMe = aList[:]
    
    for x in range(len(sortMe)):
        for y in range(len(sortMe) - 1):
            if (sortMe[y][1] > sortMe[y + 1][1]):
                sortMe[y], sortMe[y + 1] = sortMe[y + 1], sortMe[y]
    return sortMe

The list that it pulls from looks like this:

[['Steven', '172'], ['Jamie', '234'], ['Michael', '256'], ['Alice', '300'], ['Trevor', '93']]

Expected Output:

[['Alice', '300'], ['Michael', '256'], ['Jamie', '234'], ['Steven', '172'], ['Trevor', '93']]

I just need to know what I did wrong and how I'm meant to fix it, without altering the program all that much. Any assistance is greatly appreciated.

you can use sorted :
By scores

descending order: sorted(a,key=lambda l:int(l[1]), reverse=True)
ascending order: sorted(a,key=lambda l:int(l[1]))

By names

descending: sorted(a,key=lambda l:l[0], reverse=True)
ascending: sorted(a,key=lambda l:l[0])

Here is the solution for your query

aList=[['Steven', '172'], ['Jamie', '234'], ['Michael', '256'], ['Alice', '300'], 
        ['Trevor', '93']]
def sortListScore(aList):
    score=[]
    for item in aList:
        score.append(int(item[-1]))
    score.sort()
    sorted_list=[]
    for i in range(len(score)):
        for a in aList:
            if(score[i]==int(a[-1])):
                sorted_list.append(a)
    print(sorted_list[::-1])
sortListScore(aList)

Let me explain you the logic:

Firstly, I'm appending the numerical scores from the list given to another list (say score). Now I'm sorting the new list(score). So the order of numerical scores is sorted. Next, I'm finding the item of the main list(list) where the scores are present and sorting them according to the score. As they are in an ascending order, I'm reversing the string and finally getting the expected output. Here's how it works,

[['Alice', '300'], ['Michael', '256'], ['Jamie', '234'], ['Steven', '172'], ['Trevor', '93']]

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