[Newbie] How to output dictionary sorted on content (not keys)
Eric Brunel
eric.brunel at pragmadev.com
Thu Jul 4 08:44:28 EDT 2002
Achim Domma wrote:
> not tested, but something like this should work:
>
> // can this be done with lambda ???
> def myComp(a,b): return cmp(a[1],b[1])
> tmpList = yourDict.items()
> tmpList.sort(myComp)
>
> for name,count in tmpList: print name,count
Here is an almost one-liner version:
l = [(i[1], i[0]) for i in yourDict.items()]
l.sort()
or:
l = zip(yourDict.values(), yourDict.keys())
l.sort()
I don't which one of these two or Achim's one is the most efficient.
HTH
--
- Eric Brunel <eric.brunel at pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com
More information about the Python-list
mailing list