Python unordered types: NoneType ()> int () when searching for max. List

I just started with python 3 days ago. During programming, I came across a strange situation.

a = [ [5, [[1, 1, None], [None, None, None], [None, None, None]]], [5, [[1, None, 1], [None, None, None], [None, None, None]]] ] 

max(a) gives me

Traceback (last last call): File "", line 1, in TypeError: unorderable types: NoneType ()> int ()

But if I try

 a = [ [5, [[1, 1, None], [None, None, None], [None, None, None]]], [5.1, [[1, None, 1], [None, None, None], [None, None, None]]] ] 

max(a) displays

 [5.1, [[1, None, 1], [None, None, None], [None, None, None]]] 

Any specific reason for this behavior?

Update 1: I tried something else

  a = [[5, [[1,2], [3,4]]],[5,[[3,4],[5,10]]],[5,[[5,6],[7,8]]]] 

and max(a) is [5, [[5, 6], [7, 8]]] My doubt is why the error is not displayed in this case?

+5
source share
2 answers

This is because max does this when it encounters None values:

 max([1, None]) 

also gives the same error:

 --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-14-c33cf47436bc> in <module>() ----> 1 max([1,None]) TypeError: unorderable types: NoneType() > int() 

Basically max tries to iterate over the list and first find out the bigger value. But as he reaches None, he can no longer compare, thus giving an error.

with

 a = [ [5, [[1, 1, None], [None, None, None], [None, None, None]]], [5.1, [[1, None, 1], [None, None, None], [None, None, None]]] ] 

it compares 5 and 5.1 and considers the list with 5.1 as more.

While both first values โ€‹โ€‹are 5, he went on to iterate the next element and ran into None , which caused an error.

Update:

This example may help clarify the error message even better:

 max([1,'2']) 

Error:

 TypeError: unorderable types: str() > int() 

basically he tried to compare '2' with 1 and gave TypeError: unorderable types: str() > int()

earlier we compared None with int() 1 , and the error message we received was TypeError: unorderable types: NoneType() > int()

+6
source

In Python 2, this None trick, which was compared below with any integer, was useful in some cases when you need a minimum value that you could not predict (since integers do not have a fixed min / max, as in C).

In Python 3, this is no longer possible (and most of the time it is for the better, it saves a lot of headaches when comparing strings with integers like "2" and 3, for example.

If you really need it, I thought of a workaround.

You can define a class that is lower than any other object, and use an instance of this class instead of None :

 class AM: def __int__(self): return 0 def __gt__(self,other): return False def __repr__(self): return "INF" # prints out nicely always_min = AM() a = [ [5, [[1, 1, always_min], [always_min, always_min, always_min]]], [5, [[1, always_min, 1], [always_min, always_min, always_min]]] ] print(max(a)) 

I get:

 [5, [[1, 1, INF], [INF, INF, INF]]] 

which proves that the tie-break worked.

Please note that this is a minimal implementation. max uses only :: the definition only __gt__ is lazy, we need to define the other __ge__ , __le__ , __lt__ respectively for general use.

+3
source

Source: https://habr.com/ru/post/1265021/


All Articles