sorting a dictionary

Giovanni Bajo noway at sorry.com
Tue Feb 4 20:41:06 EST 2003


"Alex Martelli" <aleax at aleax.it> ha scritto nel messaggio
news:nXL%9.174528$AA2.6987601 at news2.tin.it...

> def get_highest_in_linear_time_without_explicit_iterators(d):
>     for k in d:
>         maxkey, maxval = k, d[k]
>         break
>     else:
>         raise ValueError, "empty dictionary has no highest key"
>     for k in d:
>         v = d[k]
>         if v>maxval: maxkey, maxval = k, v
>     return maxkey

Doesn't d[k] require O(logN) too? Or is Python smart enough to optimize this
away since we are going through the dictionary?
Your previous solution with explicit iterators didn't have this "issue".

Giovanni Bajo






More information about the Python-list mailing list