Error with Sorted Python 3


Good, I have an error when ordering. I pass the method and the error it gives. I modified the Sorted line and it does not give me any errors so I guess the error is there

def selection_and_reproduction(poblacionNueva):

puntuados = [(calcularFitness(i), i) for i in poblacionNueva]
puntuados = [i[1] for i in sorted(puntuados)] 
poblacionNueva = puntuados

selected =  puntuados[(len(puntuados)-pressure):]  

for i in range(len(poblacionNueva)-pressure):
    punto = random.randint(1,largo-1) 
    padre = random.sample(selected, 2) 

    poblacionNueva[i][:punto] = padre[0][:punto] 
    poblacionNueva[i][punto:] = padre[1][punto:] 

return poblacionNueva 

asked by Carlos Lozano 12.07.2017 в 18:14

1 answer


The error is because you are trying to sort a list that contains None and integer values. This is not possible in Python 3 and is one of its differences with Python 2.

Free translation of the documentation :


The order comparison operators ( < , <= , >= , > ) generate an exception TypeError when the operands do not have a significant natural order. Therefore, expressions such as 1 < '' , None > None or len <= len are no longer valid, and for example, None < None generates a TypeError instead of returning False . The conclusion is that ordering a heterogeneous list no longer makes sense, all elements must be comparable to each other. Note that this does not apply to operators == and != . The incomparable objects of different types will always be unequal to each other.

This is possible in Python 2 , it is not possible in Python 3 and, for me, with good reason. An object None may in our representation of reality may be 0 or '' or may not. An empty string or 0 is not always "equivalent" to anything. Strictly speaking neither an empty string nor 0 are "nothing".

In Python 2 0 > None returns True , which at least makes us think about the meaning of "0", of "nothing", of "infinite", of life, etc ... XD. Out of jokes, this is ambiguous, as is comparing "" > 0 or None > None .

Your list puntuados should be something like:

[(1, 2), (3, None), (None, 4)]

sorted when ordering uses the operator < and when it is found that it has to evaluate if an integer is or not greater than None throws the exception according to the above.

It has a solution; or by eliminating the values None or you change them by an equivalent value that does not change the sense or the result of your code (there are times that can be changed by 0 without problems). You can also do this without altering the original list using the argument key of sorted knowing the structure of your list.

However, to be able to give you an option it would be necessary to know how you consider those values you. You must consider if for your algorithm they are equivalent to 0 or they are smaller than any integer. To see how relative this is (and why it is corrected in Python 3), if we understand that None is less than any integer, how should we consider None with respect to -∞ ?

answered by 12.07.2017 / 20:11