[Tutor] Sorting tuples

Mike Nickey mnickey at gmail.com
Thu Jun 21 01:27:57 CEST 2012


While i'm still not sure what lamda is or represents, I've found a solution.
Thank you all for your assistance, past, present and future. :)

This seems to work:
def sort_last(tuples):
    return sorted(tuples, key=myDef)

def myDef(s):
    return s[-1]


On Wed, Jun 20, 2012 at 4:05 PM, Mike Nickey <mnickey at gmail.com> wrote:
> Hey all,
>
> I'm working through the google classes to increase my python
> understanding and I'm stuck on sorting tuples.
> What is being asked is to sort tuples based on the second element of
> the tuple in ascending order.
>
> Given a list of non-empty tuples, return a list sorted in increasing
> order by the last element in each tuple.
> e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields [(2, 2), (1, 3), (3,
> 4, 5), (1, 7)]
>
> What I have is
> def sort_last(tuples):
>    # +++your code here+++
>    print sorted(tuples, key=lamda tuples: tuples[-1])
>    return
>
> I'm stuck and don't really understand what lamda is. From various
> web-boards I have seen that sorting on -1 should give me the last
> element o each sub-list.  Can anyone assist?
>
> --
> ~MEN



-- 
~MEN


More information about the Tutor mailing list