RE: Dict Copy & Compare
- From: "Hamilton, William " <whamil1@xxxxxxxxxxx>
- Date: Mon, 30 Apr 2007 12:50:58 -0500
-----Original Message-----[mailto:python-
From: python-list-bounces+whamil1=entergy.com@xxxxxxxxxx
list-bounces+whamil1=entergy.com@xxxxxxxxxx] On Behalf Of RobertRawlins -
Think Blue
Sent: Monday, April 30, 2007 6:09 AM
To: 'Tim Golden'
Cc: python-list@xxxxxxxxxx
Subject: RE: Dict Copy & Compare
On quick question, how can I order a dict by the 'values' (not keys)
before
looping? Is that possible?
The easiest way I can think of is to create a new dict that's reversed.
reverseDict = {}
for key in dict1:
if dict1[key] not in reverseDict:
reverseDict[dict1[key]]=[key]
else:
reverseDict[dict1[key]].append(key)
This gives you a dictionary that has the old dict's values as keys, and
the old dict's keys as lists of values. You can then sort the keys of
this dict and do what you want with it. Of course, the values in dict1
have to be valid dictionary keys for this to work. If they aren't, you
may be able to get away with converting them to strings.
---
-Bill Hamilton
.
- Prev by Date: re-importing modules
- Next by Date: Re: regexp match string with word1 and not word2
- Previous by thread: RE: Dict Copy & Compare
- Next by thread: Importing a csv file
- Index(es):
Relevant Pages
|