mirror of
https://github.com/rasbt/python_reference.git
synced 2025-03-28 16:36:48 +00:00
Sorting a list of tuples by the last last elements of the tuple
This commit is contained in:
parent
841401ab65
commit
b6f54765e7
@ -1,6 +1,15 @@
|
|||||||
# Sebastian Raschka 09/02/2014
|
# Sebastian Raschka 09/02/2014
|
||||||
# Sorting a list of tuples by the last last elements of the tuple
|
# Sorting a list of tuples by the last last elements of the tuple
|
||||||
|
|
||||||
|
|
||||||
|
# Here, we make use of the "key" parameter of the in-built "sorted()" function
|
||||||
|
# (also available for the ".sort()" method), which let's us define a function
|
||||||
|
# that is called on every element that is to be sorted. In this case, our
|
||||||
|
# "key"-function is a simple lambda function that returns the last item
|
||||||
|
# from every tuple.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
a_list = [(1,3,'c'), (2,3,'a'), (1,2,'b')]
|
a_list = [(1,3,'c'), (2,3,'a'), (1,2,'b')]
|
||||||
|
|
||||||
sorted_list = sorted(a_list, key=lambda e: e[::-1])
|
sorted_list = sorted(a_list, key=lambda e: e[::-1])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user