![]() However, this reverse setting applies to the entire sort. It's equivalent to sorting normally, and then reversing the result, but more efficient. Meanwhile, the reverse keyword argument for sorting can specify that sorting should be done in reverse order. These tuples will sort lexicographically, so this produces the desired result - elements are sorted according to the first key result, with ties broken by the second, etc. In general, a key function for sorting can simply produce a tuple, where each element corresponds to one of the "keys" we want to use for sorting. I have created a new Python Project called multisort which exposes three methodologies:Īll of the following applies to both the built-in sorted function, and the. Although, this does multiple sorted calls, it is hands down the fastest multi-sort method!. ![]() ![]() The is None, check is defensive check so that search does not fail on None values.Students_sorted.sort(key=key_grade, reverse=True) Students_sorted = sorted(students, key=key_attend) Here is a NoneType safe version: students = [ When using Python 3, made an excellent observation that the multisort method listed in the current python docs is incredibly fast and be used to accomplish multi-colum sort with discrete ordering. Or, using lambda to avoid writing function: sortedList = sorted(Ĭmp=lambda (a1, b1), (a2, b2): 0 if (a1, b2) = (a2, b1) else 1 if (a1, b2) > (a2, b1) else -1 SortedList = sorted(myList, cmp=compare_func) I recommend against this approach as it is messy and the cmp keyword is not available in Python 3.Īnother approach is to swap the elements when comparing the elements: def compare_func(x, y): If your elements are numbers, you can cheat a little: sorted(myList, key=lambda(a,b):(a,1.0/b)) SortedList = sorted(tempList, key=lambda(a,b):a)) Or break down: tempList = sorted(myList, key=lambda (a,b):b, reverse=True) SortedList = sorted(myList, key = case_insensitive_and_2nd_reversed)Ī simple solution, but might not be the most efficient is to sort twice: the first time using the second element, the second using the first element: sortedList = sorted(sorted(myList, key=lambda (a,b):b, reverse=True), key=lambda(a,b):a) Return self.first != other.first and cond != cond Return self.first = other.first and cond = cond Return self.first other.first or self.first = other.first and cond >= cond Return self.first other.first or self.first = other.first and cond > cond In your case, since lower-casing is relatively expensive, you might want to do a combination: class case_insensitive_and_2nd_reversed: (You can see a simple definition of cmp_to_key at the end of the Sorting How-To In 3.2, cmp_to_key was added to functools it creates keys from the original objects by wrapping them in an object whose comparison function is based on the cmp function. There was a cmp argument to sorted from its introduction to 2.4, but it was removed from Python 3 in favour of the more efficient key function. Sometimes there is little alternative but to use a comparator function.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |