How does tuple comparison work in Python?

前端 未结 4 1663
梦毁少年i
梦毁少年i 2020-11-22 04:26

I have been reading the Core Python programming book, and the author shows an example like:

(4, 5) < (3, 5) # Equals false

So,

4条回答
  •  名媛妹妹
    2020-11-22 05:13

    The python 2.5 documentation explains it well.

    Tuples and lists are compared lexicographically using comparison of corresponding elements. This means that to compare equal, each element must compare equal and the two sequences must be of the same type and have the same length.

    If not equal, the sequences are ordered the same as their first differing elements. For example, cmp([1,2,x], [1,2,y]) returns the same as cmp(x,y). If the corresponding element does not exist, the shorter sequence is ordered first (for example, [1,2] < [1,2,3]).

    Unfortunately that page seems to have disappeared in the documentation for more recent versions.

提交回复
热议问题