PDA

View Full Version : Python code question




rien333
Jun 29, 2011, 11:32 AM
I've been working on a project in Python today, but I can't find a solution for a problem I encountered a few hours ago. It would be great if I can finish my project tomorrow and I thought maybe someone around here is able to help me, because I just can't figure out a way to do the following thing myself:
Here is the code: a_list =[[1,2,3,"I"],[4,7,"Cl"],[8,"Br"],[9,10,"F"]]
What I want, if it is possible at all, is that this list or a similar list becomes sorted alphabetically like this:
a_list = [[8,"Br"],[4,7,"Cl"],[9,10,"F"],[1,2,3,"I"]]
Hope I'm clear enough (For some reason I don't think I am...).
Hope one of your guys is able to help me! Or point me at some sort of direction.



ehoui
Jun 29, 2011, 11:53 AM
Look up the "sorted" built-in function. Your lambda function is going to look at the last item in the list:

sorted(a_list, key = lambda x: x[-1] )

rien333
Jun 29, 2011, 12:01 PM
Wow, thanks! I really appreciate your help!