In some recent python code I was playing with I had the need to sort a bunch of lists based on elements within their elements. Thankfully python's built in sort function is plenty powerful and allows us to do this in a fairly easy manner. So for example if we have a list all of whose elements are also lists: mylist = [["derp", 1, 7], ["bleh", 2, 0], ["merp", 0, 3]]By default when we call the sort function on this list it will sort the sublists based on their first element in lexicographic order. For example: mylist.sort()print mylist[['bleh',...
Thursday, 6 June 2013
Subscribe to:
Posts (Atom)