Issue
In the following code below this is a simple algorithm written to sort the elements.My question is that how are strings are compared internally and how the interpreter knows that these strings are to be placed after the integers
a=[22, 66, 54, 11, 16, 2, 5, 'b', 'a', 3, 2, 1]
>>> for i in range(len(a)-1):
... for j in range(len(a)-i-1):
... if a[j] > a[j+1]:
... a[j],a[j+1]=a[j+1],a[j]
...
>>> print a
[1, 2, 2, 3, 5, 11, 16, 22, 54, 66, 'a', 'b']
Solution
In 2.x, if the two objects cannot be coerced to a common type then it compares the class names. "str" > "int", so they come after.
In 3.x, if the two objects cannot be coerced to a common type then an exception is raised.
Answered By - Ignacio Vazquez-Abrams
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.