RE: Dict Copy & Compare



-----Original Message-----
From: python-list-bounces+whamil1=entergy.com@xxxxxxxxxx
[mailto:python-
list-bounces+whamil1=entergy.com@xxxxxxxxxx] On Behalf Of Robert
Rawlins -
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
.



Relevant Pages

  • Re: Help With PyParsing of output from win32pdhutil.ShowAllProcesses()
    ... None to a string). ... Still have an urge to parse with pyparsing? ... pyparsing Dict class. ... keys from the parsed data, so it looks like you are already thinking ...
    (comp.lang.python)
  • Re: Dictionary .keys() and .values() should return a set [with Python 3000 in mind]
    ... keyset of a dictionary be represented as a set instead of a list? ... is as much a product of historical progression as anything: lists and dicts ... so the collection of keys had to be returned as a list. ... of the elements in the dict.keys() list had no bearing on the dict, ...
    (comp.lang.python)
  • Re: Floats as keys in dict
    ... store the nodes I have in play as keys in a dict. ... the dict keys are then floats and I have to round the values ... give the same hash for two numbers that are "close enough" (e.g., ...
    (comp.lang.python)
  • Re: Sets in Python
    ... allow mutable items as keys is a bit arbitrary. ... The user doesn't need to know the mechanism, but the dict does. ... implemented as hash tables. ... Allow the hash of mutable objects to change, ...
    (comp.lang.python)
  • Re: Why are tuples immutable?
    ... If hash equals id, then the first of those cases fails. ... to key the dict. ... I need to know the order of the keys in that list. ... >keys being mutable objects, that they want to mutate those object while ...
    (comp.lang.python)