Having a problem recently when using the max() function in python. Here is my code:
x = ["AJK","exit","Down","World","HappyASD"]
max(x)
But instead of getting "HappyASD", I get "exit". Any help?
Having a problem recently when using the max() function in python. Here is my code:
x = ["AJK","exit","Down","World","HappyASD"]
max(x)
But instead of getting "HappyASD", I get "exit". Any help?
If you are looking for the max by length, you need to send the key to the max
function
x = ["AJK","exit","Down","World","HappyASD"]
max(x, key=len)
DEMO:
>>> x = ["AJK","exit","Down","World","HappyASD"]
>>> max(x, key=len)
'HappyASD'
>>>
If the key is not specified, by default the max is determined by the coerced type.
can see no error in here... ord values of first letter in exit
are the highest
It is because the max() compares the ascii value of the character. I think. 'e' is 0x65, while, 'A' is 0x41, 'D' is 0x44 and so on.
you can refer to: http://www.tutorialspoint.com/python/string_max.htm