Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python sorting by multiple criteria

Tags:

python

sorting

I have a list where each element is of the form [list of integers, integer]. For example, an element of the list may look like this [[1,3,1,2], -1].

I want to sort a list containing the described type of elements by the following criteria:

  1. if the integer lists of two elements (i.e. element[0]) are of different length, the element with the smaller integer list is the smaller one.

  2. else if the integer lists are of the same length, the smaller element is that which has the smaller integer for the first integer which differs in the integer list of both elements. For example:

    [[1,1,99,100], -1] < [[1,1,100,1], -1], because 99 < 100.

  3. else if the integer lists are identical, the smaller element is the one with the smaller integer in element[1].

How would I write an approriate key function I could pass to sorted() or sort()?

like image 415
timgeb Avatar asked Nov 22 '13 13:11

timgeb


People also ask

How do you sort multiple criteria in Python?

So when you want to sort on multiple criteria in order of preference, just stick each criterion into an element of a tuple, in the same order.

How do you sort by condition in Python?

In lis. sort(key=len) , the key argument must be a function that takes in 1 argument. Whatever this function returns will be the condition that Python will sort our list by.

How do you sort multiple values in a dictionary Python?

Sort Dictionary Using the operator Module and itemgetter() This function returns the key-value pairs of a dictionary as a list of tuples. We can sort the list of tuples by using the itemgetter() function to pull the second value of the tuple i.e. the value of the keys in the dictionary.

How do you sort a list by double in Python?

To sort a two-dimensional list in Python use the sort() list method, which mutates the list, or the sorted() function, which does not. Set the key parameter for both types using a lambda function and return a tuple of the columns to sort according to the required sort order.


2 Answers

List the three criteria in your key:

sorted(inputlist, key=lambda e: (len(e[0]), e[0], e[1]))

Now you are sorting each element first by the length, then by comparing the first element directly (which in only used when the first element is of equal length), then by the value of the last integer.

Python sorts tuples and lists like these lexicographically; compare the first element, and only if that doesn't differ, compare the second element, etc.

The second element here is e[0] which will only be used if both compared entries have nested lists of equal length. These are again compared lexicographically, so pairing up elements until a pair differs.

like image 83
Martijn Pieters Avatar answered Sep 29 '22 01:09

Martijn Pieters


key = lambda x: (len(x[0]), x[0], x[1])

This works because tuples (and lists) are compared by looking at each element in turn until a difference is found. So when you want to sort on multiple criteria in order of preference, just stick each criterion into an element of a tuple, in the same order.

I'm assuming that by "smaller" you mean the same thing as "lesser", that is to say you don't mean to compare absolute values.

like image 17
Steve Jessop Avatar answered Sep 29 '22 02:09

Steve Jessop