Note that your expression is rather verbose; True if a.mentioned > b.mentioned else False
could just be simplified to a.mentioned > b.mentioned
; the >
operator already produces either True
or False
. Using simple integers you can see that that is not going to produce expected results:
>>> sorted([4, 2, 5, 3, 8], cmp = lambda a, b: a > b)[4, 2, 5, 3, 8]
while actually returning -1
, 0
, or 1
does work:
>>> sorted([4, 2, 5, 3, 8], cmp = lambda a, b: 1
if a > b
else 0
if a == b
else - 1)[2, 3, 4, 5, 8]
or instead of such a verbose expression, just use the built-in cmp()
function; for your case you'd use that like this:
srt = sorted(twitter_users.values(), cmp = lambda a, b: cmp(a.mentioned, b.mentioned))
Because you are only accessing an attribute, instead of a lambda
you can use a operator.attrgetter()
object to do the attribute fetching for you:
from operator
import attrgetter
srt = sorted(twitter_users.values(), key = attrgetter('mentioned'))
1 week ago If list is not sorted as expected, it means that the citations at the beginning of the list are missing the sorted information. Example: If the sorting by 'Title' doesn't work, when opening the 'Citation Details' tab, you'll see that the 'Title' field is empty, though the resource record has a title. , 5 days ago May 31, 2016 · If list is not sorted as expected, it means that the citations at the beginning of the list are missing the sorted information. Example: If the sorting by 'Title' doesn't work, when opening the 'Citation Details' tab, you'll see that the 'Title' field is empty, though the resource record has a title. , 1 week ago I concur that sorting a picklist field (or any field) with only the records on the currently-displayed page and not the entire dataset is awkward and often misleading. At the very least, I’d like to see an annotation signaling the sort applies only to records appearing on the current page, and ideally I’d like to see the sort apply to all ... , 1 week ago Jun 15, 2022 · 1. Change the Cell Format to Sort Date. We can solve this sort by date problem in Excel by changing the cell format. Step 1: Select all the cells first. Press the right button of the mouse. Choose Format Cells from the options. You can also go to the Format cells option by using the keyboard shortcut CTRL + 1.
srt = sorted(twitter_users.values(), cmp = (lambda a, b: True
if a.mentioned > b.mentioned
else False)) for s in srt: print s.mentioned
>>> sorted([4, 2, 5, 3, 8], cmp = lambda a, b: a > b)[4, 2, 5, 3, 8]
srt = sorted(twitter_users.values(), cmp = (lambda a, b: True
if a.mentioned > b.mentioned
else False)) for s in srt: print s.mentioned
>>> sorted([4, 2, 5, 3, 8], cmp = lambda a, b: a > b)[4, 2, 5, 3, 8]
>>> sorted([4, 2, 5, 3, 8], cmp = lambda a, b: 1
if a > b
else 0
if a == b
else - 1)[2, 3, 4, 5, 8]
srt = sorted(twitter_users.values(), cmp = lambda a, b: cmp(a.mentioned, b.mentioned))
Return value: The method list.sort() returns None, no matter the list on which it’s called. Why? Because it sorts a list in-place and doesn’t create a new list.,The method list.sort() sorts the given list in place. It doesn’t create a new list.,However, returning None makes perfect sense for the list.sort() method. Why? Because you call the method on a list object and it modifies this exact list object. It doesn’t create a new list—there won’t be a new list object in memory. ,The return value of the list.sort() method is None, but many coders expect it to be the sorted list. So they’re surprised finding out that their variables contain the None type rather than a sorted list.
Here’s a short overview example that shows you how to use the arguments in practice:
# Create an unsorted integer list lst = [88, 12, 42, 11, 2] # Sort the list in place(ascending) lst.sort() print(lst) #[2, 11, 12, 42, 88] # Sort the list(leading number) lst.sort(key = lambda x: str(x)[0]) print(lst) #[11, 12, 2, 42, 88] # Sort the list in place(descending) lst.sort(reverse = True) print(lst) #[88, 42, 12, 11, 2]
Now you know the basics. Let’s deepen your understanding with a short code puzzle—can you solve it?
# Create an unsorted integer list lst = [88, 12, 42, 11, 2] # Sort the list in place(ascending) lst.sort() print(lst) # Sort the list(leading number) lst.sort(key = lambda x: str(x)[0]) print(lst) # Sort the list in place(descending) lst.sort(reverse = True) print(last) # What 's the output of this code snippet?
Here’s an example:
>>> lst = [(1, 2), (3, 2), (3, 3), (1, 0), (0, 1), (4, 2), (1, 1), (0, 2), (0, 0)] >>> lst.sort() >>> lst[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (3, 2), (3, 3), (4, 2)] >>> lst.sort(key = lambda x: x[0]) >>> lst[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (3, 2), (3, 3), (4, 2)] >>> lst.sort(key = lambda x: x[1]) >>> lst[(0, 0), (1, 0), (0, 1), (1, 1), (0, 2), (1, 2), (3, 2), (4, 2), (3, 3)]
So to sort with “two keys”, you can define a key function that returns a tuple rather than only a single tuple value. Here’s an example:
>>> lst = [(1, 2), (3, 2), (3, 3), (1, 0), (0, 1), (4, 2), (1, 1), (0, 2), (0, 0)] >>> lst.sort(key = lambda x: (x[1], x[0])) >>> lst[(0, 0), (1, 0), (0, 1), (1, 1), (0, 2), (1, 2), (3, 2), (4, 2), (3, 3)]
The sort() method has no return value and directly modifies the original list, changing the order of the elements contained in it.,The sort() method returns None, which means there is no return value since it just modifies the original list. It does not return a new list.,The return value will then apply as the sorting criteria for the list.,For example, you can create a specific function and then sort the list according to the return value of that function.
The general syntax for the sort()
method looks like this:
list_name.sort(reverse = ..., key = ...)
The general syntax to do this would look something similar to the following:
list_name.sort()
Let's take a look at the following example which shows how to sort a list of whole numbers:
# a list of numbers my_numbers = [10, 8, 3, 22, 33, 7, 11, 100, 54] #sort list in -place in ascending order my_numbers.sort() #print modified list print(my_numbers) #output #[3, 7, 8, 10, 11, 22, 33, 54, 100]
The general syntax to do this would look something like this:
list_name.sort(reverse = True)
Let's reuse the same example from the previous section, but this time make it so the numbers are sorted in reverse order:
# a list of numbers my_numbers = [10, 8, 3, 22, 33, 7, 11, 100, 54] #sort list in -place in descending order my_numbers.sort(reverse = True) #print modified list print(my_numbers) #output #[100, 54, 33, 22, 11, 10, 8, 7, 3]
Last Updated : 19 May, 2022
Input: [(1, 3), (3, 2), (2, 1)]
Output: [(2, 1), (3, 2), (1, 3)]
Explanation: sort tuple based on the last digit of each tuple.
Output:
Sorted: [(2, 1), (3, 2), (1, 3)]