>> "spam" < "SPAM...">

How does string comparison work in python?

Possible duplicate:
Python string comparison technique

>>> "spam" < "bacon" False >>> "spam" < "SPAM" False >>> "spam" < "spamalot" True >>> "Spam" < "eggs" True 

qeustion: strings with the same length are compared. what if the lines are the same length and why is the spam smaller than the eggs?

+4
source share
4 answers

Lexigraphically.

The first bytes are compared, and if the ordinal value of the first is less than the second, it is less. If it is more, it is more. If they match, the next byte is checked. If everything binds, and one is longer, shorter - less.

 >>> "a" < "zzz" True >>> "aaa" < "z" True >>> "b" < "a" False >>> "abc" < "abcd" True >>> "abcd" < "abce" True >>> "A" < "a" True >>> ord("A") 65 >>> ord("a") 97 
+3
source

Since A precedes A in the ASCII table, therefore S in Spam is considered less than e in eggs .

 >>> "A" < "a" True >>> "S" < "e" True >>> "S" < "eggs" True 

Please note that the string length is not considered in comparison. The ordinal values ​​for each byte are compared, starting with the first byte, as @MikeGraham rightly points out in the comments below. And as soon as a mismatch is detected, the comparison stops, and the comparative value is returned, as in the last example.

From docs - Comparison of sequences and other types : -

Comparison uses lexicographical ordering: first, the first two objects are compared, and if they differ, this determines the result of the comparison; if they are equal, the following two elements are compared, and so on, until any sequence is exhausted.

Also in the same paragraph: -

Lexicographic ordering for strings uses ASCII order for single characters

+2
source

Lines in Python are lexicographically ordered so that they can be logically sorted:

 >>> print sorted(['spam','bacon','SPAM','spamalot','Spam','eggs']) ['SPAM', 'Spam', 'bacon', 'eggs', 'spam', 'spamalot'] 

There are tradeoffs with this, especially with unicode. The letter Γ© will be sorted after the letter z for example :

 >>> 'e' < 'z' True >>> 'Γ©' < 'z' False 

Fortunately, you can use the sort function, use locale or a subclass of the string to sort the strings anyway.

+2
source

This is a lexicographic comparison.

+1
source

Source: https://habr.com/ru/post/1446261/


All Articles