The comparison uses lexicographical ordering: first the first two
items are compared, and if they differ this determines the outcome of
the comparison; if they are equal, the next two items are compared,
and so on, until either sequence is exhausted.
同样在同一段中:
Lexicographical ordering for strings uses the ASCII ordering for
individual characters
词汇上。在
比较第一个字节,如果第一个字节的序数值小于第二个字节的序数值,那么它就更小。如果它更多,它就更大。如果它们相同,则尝试下一个字节。如果都是领带,一条长一点,短一点的就少一点。在
由于ASCII表中
A
在a
之前,因此Spam
中的S
被认为小于eggs
中的e
。在请注意,在比较中不考虑字符串长度。正如@MikeGraham在下面的注释中正确指出的那样,从第一个字节开始比较每个字节的顺序值。 一旦发现不匹配,就会停止比较,并返回比较值,如上一个示例所示。在
来自docs - Comparing Sequences and Other Types:
同样在同一段中:
Python中的字符串是lexicographically ordered,因此可以对它们进行逻辑排序:
unicode的主要缺点是。字母
^{pr2}$é
将在字母z
for example之后排序:幸运的是,您可以使用sort函数,使用
locale
或string的子类对字符串进行排序。在相关问题 更多 >
编程相关推荐