What is the __lt__ actually doing for lists [duplicate]

房东的猫 提交于 2019-12-30 22:58:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!