简体   繁体   English

字符串比较同时包含数字和字母时如何工作?

[英]How do string comparisons work when they contain both numbers and letters?

I'm trying to compare time in Python, and came up with some weird comparisons. 我正在尝试比较Python中的时间,并提出了一些奇怪的比较。 I've got no idea how the following statements work: 我不知道以下语句如何工作:

>>> "17:30" > "16:30"
True
>>> "12:30" > "13:30"
False
>>> '18:00 - asdfj' > '16:30 - asdfj'
True

My guess is that it takes the first number from before the colon, I'm not completely sure about it. 我的猜测是,它需要冒号之前的第一个数字,我不确定。

Basically, in python, it is lexicographical comparison. 基本上,在python中,它是字典比较。

Example 'a' comes before 'b', hence 'a' < 'b' is true. 示例“ a”在“ b”之前,因此“ a” <“ b”为真。 Similarly '2' < '3'. 同样,'2'<'3'。 Hence '199' < '2' is true because 1 comes before 2. 因此,'199'<'2'是正确的,因为1在2之前。

As others have pointed out, a comparison between strings is a question of lexicographical ordering. 正如其他人指出的那样,字符串之间的比较是字典顺序的问题。

What that means procedurally: 在程序上意味着什么:

  • two strings are compared one character at a time 两个字符串一次比较一个字符
  • the first character that's different decides which string is 'greater than' the other 第一个不同的字符决定哪个字符串比另一个字符串“更大”
  • if no characters are different and the strings are the same length, they are 'equal'. 如果没有字符不同且字符串长度相同,则它们“相等”。
  • if two characters are different, their 'ordinal value' decides which is 'greater' 如果两个字符不同,则它们的“常规值”决定哪个更大
  • a character is 'greater than' no character 一个字符比没有字符“伟大”

For example, 'ab' > 'a' is True , because 'a' == 'a' , but the first string has an extra character. 例如, 'ab' > 'a'True ,因为'a' == 'a' ,但是第一个字符串有一个额外的字符。 And 'abc' < 'abd' because 'c' < 'd' . 还有'abc' < 'abd'因为'c' < 'd'

'a' < 'b' because ord('a') < ord('b') . 'a' < 'b'因为ord('a') < ord('b') The ordinal value of a character is typically its ASCII value for normal characters, or more precisely, its the Unicode code point ( https://docs.python.org/3/library/functions.html#ord ). 字符的序数值通常是普通字符的ASCII值,或更确切地说,是Unicode代码点( https://docs.python.org/3/library/functions.html#ord )。 This also means that 'A' < 'a' , because uppercase letters come before lowercase letters in Unicode. 这也意味着'A' < 'a' ,因为在Unicode中大写字母位于小写字母之前。 And '1' < 'A' because numbers come before letters. '1' < 'A'是因为数字位于字母之前。

Note that this may sometimes give surprising results (note the dots on the Ӓ ): 请注意,有时这可能会产生令人惊讶的结果(请注意Ӓ上的点):

>>> 'Ӓ' > 'a'
True
>>> 'A' > 'a'
False

There are many online tables and overviews of Unicode, but here's a fairly plain example: https://www.tamasoft.co.jp/en/general-info/unicode.html 有许多在线表和Unicode概述,但这是一个非常简单的示例: https : //www.tamasoft.co.jp/en/general-info/unicode.html

As for your example: 至于你的例子:

>>> '18:00 - asdfj' > '16:30 - asdfj'
True

This makes sense, because '8' > '6' - the rest of the string doesn't matter. 这是有道理的,因为'8' > '6' 6'-字符串的其余部分无关紧要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在python中删除同时包含字母和数字的字符串 - remove string that contain both letters and numbers in python 如何检查字符串是否严格包含字母和数字 - How to check if a string is strictly contains both letters and numbers 除了字母和数字,我如何正确循环尝试? - How do I properly loop try except with both letters and numbers? 在拆分它们之前如何避免包含字符串和数字的行? - How to avoid lines that contain both string and numbers before splitting them? 如何过滤字符串以仅包含字母? - How do you filter a string to only contain letters? 如何将同时包含字母和数字的单词转换为仅数字,以便K-Neighbors分类器可以训练它对它们进行分类? - How should I convert words that contain both letters and numbers into only numbers so that K-Neighbors classifier can train it to classify them? 如何对同时包含数字和字母的列表进行排序? - How to sort a list that contains both numbers and letters? 您如何在python中编写正则表达式,以查找仅包含字母,数字和下划线的所有单词? - How do you write a regex in python that finds all word which contain only letters, numbers and underscore? 验证数字和字母 - Validate both numbers and letters Python 中的链式比较实际上如何工作? - How do chained comparisons in Python actually work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM