tags:

views:

85

answers:

6
+1  A: 
max(map(int, MyCount))

Or if you want the return value to be the original string:

max(MyCount, key=int)
Matthew Flaschen
Calling `dict.keys` is here (and most places) unnecessary and potentially inefficient.
Mike Graham
+8  A: 

This is because u'9' > u'10', since they are strings.

To compare numerically, use int as a key.

max(MyCount.keys(), key=int)
KennyTM
Calling `dict.keys` is usually superfluous.
Mike Graham
+1  A: 

Since your keys are strings, they are compared lexicographically and '9' is the max value indeed.

What you are looking for is something like:max(int(k) for k in MyCount)

Marek Rocki
+1  A: 

This is your problem:

>>> u'10' > u'9'
False

Effectively, you're comparing the characters '1' and '9' here. What you want is probably this:

max(long(k) for k in MyCount)

or create the dictionary with numbers as keys (instead of strings).

AndiDog
A: 

You use max for string values. You must convert them to int. Try something like:

print(max([int(s) for s in MyCount.keys()]))

Or as Tim suggested:

print(max(int(s) for s in MyCount))
Michał Niklas
You need neither the square brackets nor the `.keys()` method.
Tim Pietzcker
Thanks, answer updated.
Michał Niklas
+3  A: 

You need to compare the actual numerical values. Currently you're comparing the strings lexigraphically.

max(MyCount, key=int)
Mike Graham
the only pythonic answer.
SilentGhost