This question already has answers here:
How are strings compared?
(7 answers)
Closed 7 years ago.
I have numerical values that are loaded from a JSON object and are therefore all strings.
I am having issues with making numerical comparisons with these strings. The following makes no sense to me and I was hoping one of you champions could explain..
In[2]: print '100' < '45'
True
In[3]: print '99' < '45'
False
Using Python 2.7
When comparing strings they're compared by the ascii value of the characters. '1' has a value 49, and '4' is 52. So '1' is < '4'. '9' however is 57, so '9' is > '4'.
If you want to compare them numerically you could just int() the strings first like:
print int('100') < int('45')
It basically checks for lexicographic ordering.
Check documentation here -
>>> 'b' <'a'
False
>>> 'a' < 'b'
True
In above example, a comes before b, hence 'a' <'b' is true. But, not vica versa. Similarly '1'<'2'.
Hence '199999999999' < '5' is true because 1 comes before 5.
Related
I'm trying to compare time in Python, and came up with some weird comparisons. 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.
Example 'a' comes before 'b', hence 'a' < 'b' is true. Similarly '2' < '3'. Hence '199' < '2' is true because 1 comes before 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. And 'abc' < 'abd' because 'c' < 'd'.
'a' < 'b' because 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). This also means that 'A' < 'a', because uppercase letters come before lowercase letters in Unicode. And '1' < 'A' because numbers come before letters.
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
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.
when i compared tow percentage number it gives me wrong answer
a="{0:%}".format(85/100)
b="{0:%}".format(9/100)
if b>a :
print("done")
it should be pass if condition while its give me done in answer
when i compare two percentage numbers it gives me wrong answer
No, the interpreter gives the correct answer.
The variables get these string values:
a = '85.000000%'
b = '9.000000%'
You are complaining about this string comparison result:
>>> '9' > '85'
True
Or more simply, since they differ in the 1st character, about this result:
>>> '9' > '8'
True
If you would prefer a numeric comparison,
then strip the percent and recover the numbers:
>>> float(b[:-1]) > float(a[:-1])
False
This question already has answers here:
How are strings compared?
(7 answers)
Closed 6 years ago.
I have written this code in python 3.5
x="absx"
o="abcdef"
and if i am doing this operation,
x<o
False #it return's False and i think it should return True
So what is '<' doing in case of strings and why is it not returning true.
how is it comparing x and o?
The < or > would result in lexicographic comparison of the two strings:
>>> x="absx"
>>> o="abcdef"
>>> x > o
True
Lexicographic ordering is same as dictionary ordering, and basically, the operators are checking for which string would come earlier (or later) in a dictionary order. The behavior is same for both Python 2 and 3.
The final result does not depend on the size of the string, Example:
>>> "a" < "aaaaa"
True
In example above "a" would come before "aaaaa" when written in dictionary order. To compare by length of strings, use the len() function on the strings.
Lexicographic comparison. In your case o would come after x.
This question already has answers here:
How are strings compared?
(7 answers)
Closed 4 years ago.
This is another huge trap I've met today.
I spend hours on debugging my code and finally I found it caused by this weird setting
Below is my python prompt interface
'3' > '2'
True
'4' > '3'
True
'15' > '11'
True
'999233' > '123'
True
# At this point, you must think compare string numbers is just like compare numbers.
# Me too, but...
'5' > '15'
True
# What's this !!!???
# Meanwhile I am asking this question. I want to something exaggerated to mockerying
# this mechanism, and I find something surprised me:
'5' > '999233'
False
# What!!!???
# Suddenly an idea come across my mind, are they comparing the first string number
# at first, if they are equal and then compare the second one?
# So I tried:
'5' > '13333333333333333'
True
'5' > '61'
False
# That's it.
# my old doubt disappeared and a new question raised:
Why they designed such a mechanism instead of use natural number comparison mechanism?
What's the benefit to use this mechanism in "string number" comparison?
You are comparing Strings not numbers.
20 > 9 evaluates True for numeric types like integers and floats but with lexicographical comparison (strings) then '20' < '9' evaluates to True
Example:
$ python
>>> 5 > 10
False
>>> '5' > '10'
True
>>> '05' > '10'
False
>>> 'abc05' > 'bca10'
False
>>> 'dog' > 'cat'
True
>>> type('10')
<type 'str'>
>>> type(10)
<type 'int'>
It is a lexicographical comparison. As soon as an element greater than the other is found the comparison stops, so
'5' > '15'
is true because '5' is greater than '1'
Indeed the ascii value of the character '5' is greater than the value '1' so '5' > '15' evaluates to True. As string comparison is byte by byte, just like the length of a word in the dictionary doesn't effect it's position '5' > '1412423513515' is also True.
>>> '5' > '15'
True
>>> ord('5')
53
>>> ord('1')
49
Think of the string representation of the integers like alphabetical characters i.e 'z' > 'abc' evaluates to True because 'z' comes after 'a'. This is called lexicographic ordering.
The answer is that strings are compared from the leftmost character, or "lexicographical ordering". This gives intuitive results for, for example,
"an" < "b" # True
If you want to compare the values of the numbers the strings represent, you should be explicit about that:
int("15") < int("5") # False
You are comparing strings since your numbers are surrounded by single quotes.
In python < and > operators applied on strings compare them using lexicographic order.
You can test this putting a '0' before '5':
'05' > '11'
It's comparing the initial digit. Just as if it was alphabetical. 5 > 1 that's why you're getting 5 > 16
This question already has answers here:
How are strings compared?
(7 answers)
Closed 5 months ago.
>>> "spam" < "bacon"
False
>>> "spam" < "SPAM"
False
>>> "spam" < "spamalot"
True
>>> "Spam" < "eggs"
True
How are equal length strings compared? Why is "Spam" less than "eggs"? What if the strings are not the same length?
Lexigraphically.
The first bytes are compared and if the ordinal value of the first is less than of the second, it's lesser. If it's more, it's greater. If they are the same, the next byte is tried. If it's all ties and one is longer, the shorter one is lesser.
>>> "a" < "zzz"
True
>>> "aaa" < "z"
True
>>> "b" < "a"
False
>>> "abc" < "abcd"
True
>>> "abcd" < "abce"
True
>>> "A" < "a"
True
>>> ord("A")
65
>>> ord("a")
97
Since A comes before a in ASCII table, so S in Spam is considered smaller than e in eggs.
>>> "A" < "a"
True
>>> "S" < "e"
True
>>> "S" < "eggs"
True
Note that, String length in not considered in comparison. Rather ordinal values for each byte are compared starting from the first byte, as rightly pointed out by #MikeGraham in comments below.
And as soon as mismatch is found, the comparison stops, and comparison value is returned, as in the last example.
From the docs - Comparing Sequences and Other Types: -
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.
Also further in the same paragraph: -
Lexicographical ordering for strings uses the ASCII ordering for
individual characters
Strings 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 compromises with this, primarily with unicode. The letter é will be sorted after the letter z for example:
>>> 'e' < 'z'
True
>>> 'é' < 'z'
False
Luckily, you can use a sort function, use locale or a subclass of string to have strings sorted anyway you wish.
It is a lexicographical comparison.