Python List Sort in Descending Order

Python list sort in descending order

In one line, using a lambda:

timestamps.sort(key=lambda x: time.strptime(x, '%Y-%m-%d %H:%M:%S')[0:6], reverse=True)

Passing a function to list.sort:

def foo(x):
return time.strptime(x, '%Y-%m-%d %H:%M:%S')[0:6]

timestamps.sort(key=foo, reverse=True)

Sort a list of lists using descending order, and then if there is a tie breaker, use descending order on another index in python

It is because you are applying a negation (-) on a tuple a string. Using sorted function, you can use reverse=True to sort the array in descending order.

sorted(master_list, key=lambda x: (x[1], x[0]), reverse=True) 

How to sort integer list in python descending order

You are printing the list sorted ascending:

print sorted(ListB)

If you want it descending, put your print statement on the previous line (where you reverse it)

print sorted(ListB, key=int, reverse=True)

Then remove your final print statement.

Example:

>>> ListB = [24, 13, -15, -36, 8, 22, 48, 25, 46, -9]
>>> print sorted(ListB, key=int, reverse=True)
[48, 46, 25, 24, 22, 13, 8, -9, -15, -36]

How can i sort a list by the second item descending and first one ascending?

First sort the list by the first item, then by the second item:

list_results = sorted(list_results, key=lambda x:x[0])
list_results = sorted(list_results, key=lambda x:x[1], reverse=True)

or better yet without copying:

import operator

list_results.sort(key=operator.itemgetter(0))
list_results.sort(key=operator.itemgetter(1), reverse=True)

Python's sort algorithm is Timsort. It is a stable algorithm meaning if 2 values are the same, they'll stay in their original order.

If you sort alphabetically first, and then by the priority, the list will be sorted according to the alphabet, then re-sorted according to priority with alphabet being secondary.

How to sort a list by number first in descending order and then by alphabet in ascending order

You can use sorted with parameter key:

ff = [('o', 2), ('l', 1), ('e', 1), ('g', 2)]

output = sorted(ff, key=lambda x: (-x[1], x[0]))

print(output) # [('g', 2), ('o', 2), ('e', 1), ('l', 1)]

When a tuple is given as key, sorted sorts the list lexicographically. In this case it first sorts according to the second element in descending order (-x[1]) and then sorts according to the first element in ascending order (x[0]).

How to sort a list in descending order for the first criteria and ascending order for the second criteria?

Try this way, maybe that helps:

It works because your items are tuples so we use the tricks to sort them by desc/asceding order differently.
So the first will be descending order, and 2nd ascending as it shown.

Notes - please don't use Python built-in as the variable names.

>>> L =  [(99, 41), (85, 33), (99, 28)]
>>> sorted(L, key=lambda x: (-x[0], x[1]))
[(99, 28), (99, 41), (85, 33)]

>>> A = [(12, 22), (13, 33), (12, 12), (13, 3)]
>>> A.sort(key=lambda x: (-x[0], x[1])) # in-place sort
>>>
>>> A
[(13, 3), (13, 33), (12, 12), (12, 22)]

Python - Sort list in descending order - Type Error

You are using dict type and sorted is for the list, not dict. In order to sort a dict, you have to use a list and make a dict again or just use the list to print the highest score list.

score = {'Player1' : 9, 'Player2' : 6, 'Player3' : 7, 'Player4' : 8}
sorted_score = sorted(score.items(), key=lambda x: x[1], reverse=True)

for score in sorted_score:
print('{} : {}'.format(score[0], score[1]))

This gives the result,

Player1 : 9
Player4 : 8
Player3 : 7
Player2 : 6

You can adapt my code, i.e.

with open("test.txt", "r") as file:
scores = []
for row in file.read().splitlines():
scores.append(row.split(' : '))

sorted_score = sorted(scores, key=lambda x: x[1], reverse=True)

for score in sorted_score:
print('{} : {}'.format(score[0], score[1]))


Related Topics



Leave a reply



Submit