sorting values in dict
Victor Muslin
victor at prodigy.net
Thu Jun 14 17:36:58 EDT 2001
This may not be the shortest or most efficient, but it does the trick:
a={'a':9,'b':8,'c':7}
l=[]
for k,v in a.items():
l.append((v,k))
l.sort()
for v,k in l:
print v,k
This should produce:
7 c
8 b
9 a
On Thu, 14 Jun 2001 23:24:11 +0200, Jeroen Wolff <jwolff at knoware.nl>
wrote:
>I want to display a dict sorted by value.
>
>This is my dict:
>
>total={}
>srcAS = a string which i wil take from a file....
>Bytes = also a string which i wil take from a file...
>
>if total.has_key(srcAS):
> total[srcAS] = total[srcAS] + string.atoi(Bytes)
>else:
> total[srcAS] = string.atoi(Bytes)
>
>
>This is how i sort it by keys:
>
>keys = total.keys()
>keys.sort()
>for n in keys:
> print n, total[n]
>
>
>But i would like to have is sorted (on screen) by value....
>
>Is this possible?
>
>Jeroen
More information about the Python-list
mailing list