histogram type thingy for (unique) dict items
John Hunter
jdhunter at ace.bsd.uchicago.edu
Mon Mar 29 22:06:50 EST 2004
>>>>> "kevin" == kevin parks <kp87 at lycos.com> writes:
kevin> hi. I've been banging my head against this one a while and
kevin> have asked around, and i am throwing this one out there in
kevin> the hopes that some one can shed some light on what has
kevin> turned out to be a tough problem for me (though i am
kevin> getting closer).
kevin> i have been mucking with a lot of data in a dictionary that
kevin> looks like:
If I'm understanding you correctly, something like the following
should speed you on your way:
foo = { (5, 138, 1) : [ 0, 2, 7 ],
(7, 264, 1) : [ 0, 2, 7 ],
(9, 367, 0) : [ 0, 2, 7 ],
(5, 156, 1) : [ 0, 7, 2 ],
(8, 315, 1) : [ 0, 7, 2 ],
(8, 317, 1) : [ 0, 7, 2 ],
(1,2,3) : [4,5,6],}
# reverse the dict so that the values point to a list of keys which
# share that value, ignoring the order of the vale
rd = {} # reverse dict
for key, val in foo.items():
val.sort()
rd.setdefault(tuple(val),[]).append(key)
for key,val in rd.items():
print key,val
# make a count dictionary
countd = {}
for key, val in rd.items():
countd[key] = len(val)
print key, countd[key]
You can easily sort the count dictionary - search google for order
dictionary by values.
Hope this helps,
JDH
More information about the Python-list
mailing list