String comparison technique used by Python

前端 未结 7 2003
心在旅途
心在旅途 2020-11-21 15:50

I\'m wondering how Python does string comparison, more specifically how it determines the outcome when a less than (<) or greater than (>) op

7条回答
  •  我寻月下人不归
    2020-11-21 16:04

    Python and just about every other computer language use the same principles as (I hope) you would use when finding a word in a printed dictionary:

    (1) Depending on the human language involved, you have a notion of character ordering: 'a' < 'b' < 'c' etc

    (2) First character has more weight than second character: 'az' < 'za' (whether the language is written left-to-right or right-to-left or boustrophedon is quite irrelevant)

    (3) If you run out of characters to test, the shorter string is less than the longer string: 'foo' < 'food'

    Typically, in a computer language the "notion of character ordering" is rather primitive: each character has a human-language-independent number ord(character) and characters are compared and sorted using that number. Often that ordering is not appropriate to the human language of the user, and then you need to get into "collating", a fun topic.

提交回复
热议问题