dict invert - learning question
- From: dave <squareswallower@xxxxxxxxxxxx>
- Date: Sat, 3 May 2008 14:29:10 -0600
Hello,
here is a piece of code I wrote to check the frequency of values and switch them around to keys in a new dictionary. Just to measure how many times a certain key occurs:
def invert(d):
inv = {}
for key in d:
val = d[key]
if val not in inv:
inv.setdefault(val, [key])
else:
inv[val].append(key)
return inv
Using the methods above (I'm just a beginner) could I have written it more concisely? Any criticism/critique in the code would be greatly appreciated.
Many Thanks,
dave
.
- Follow-Ups:
- Re: dict invert - learning question
- From: Jerry Hill
- Re: dict invert - learning question
- From: mrkafk
- Re: dict invert - learning question
- From: Arnaud Delobelle
- Re: dict invert - learning question
- From: Duncan Booth
- Re: dict invert - learning question
- Prev by Date: Re: Feature suggestion: sum() ought to use a compensated summation algorithm
- Next by Date: Re: Feature suggestion: sum() ought to use a compensated summation algorithm
- Previous by thread: Re: State machine Example in Python
- Next by thread: Re: dict invert - learning question
- Index(es):