问题
Say I have two lists, and I run the following command
>>> s = [1, 2, 3]
>>> t = [1, 2, 4]
>>> s > t
False
>>> s < t
True
But if I were to run the following command
>>> s = [1, 2, 3]
>>> t = [1, 1, 4]
>>> s > t
True
>>> s < t
False
Have to admit, I'm not too familiar with the PY3 codebase. What exactly is going on in the __lt__, __le__, __gt__, __ge__, __ne__, __eq__
methods?
回答1:
The comparison is lexicographical. If you read the definition of that, you will understand everything.
Iterate the pairs of elements in order, and the first non-equal pair determines the winner of the ordering.
回答2:
It's comparing them naively, i.e. element by element. 4 > 3, but 2 > 1.
来源:https://stackoverflow.com/questions/37287340/what-is-the-lt-actually-doing-for-lists