[Tutor] histogram type thingy for dict items

kevin parks kp8 at mac.com
Mon Mar 29 16:16:39 EST 2004


John Purser (& co.)

This is wonderful. It has certainly put me on the right path and i am  
getting quite a lot of my code to
work, but only one problem, the output gives me several different  
entries for what is the
same input set set so that not all my time value pairs are accounted  
for properly. Ideally all the keys (locations/times)
that fit the same values would be grouped together so that the input of:


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 ] }

would give me the unique sorted value with all the applicable locations.

set (value of dict input) -- > all locations
(0, 2, 7) --> [(7, 264, 1), (5, 138, 1), (5, 156, 1), ,(8, 315, 1), (8,  
317, 1), (9, 367, 0)]

instead i get some locations listed for set

(0, 2, 7) # even though the output is a tuple this is actually the list  
(value) part of the input dictionary
(0, 7, 2) # i am getting locations
(2, 0, 7) # and times for all of these
(2, 7, 0) # but they are really the same set.
(7, 0, 2) # some how i need to tell python that this!

i know that if it was i list on input i can do this:

     # Now sort each individual set, but we don't want to have dupl.  
elements either so we call unique here too
     for each_event2 in all_events2:
         ndupes = unique(each_event2)
         ndupes.sort()
         outlist.append(ndupes)
     # find only the unique sets
     unique_items = unique(outlist)
     unique_item_count = 1


but i am not sure how to handle this in the dictionary scenario.




> ----------------------------------------------------------------------
>
>
> Message: 2
> Date: Mon, 29 Mar 2004 08:38:10 -0700
> From: "John Purser" <johnp at HomeLumber.com>
> Subject: RE: [Tutor] histogram type thingy for dict items
> To: "kevin parks" <kp8 at mac.com>,	<tutor at python.org>
> Message-ID:
> 	<D0DD3F40FC3F714A88D0DB43BAE3625686D241 at 01-hl- 
> prime.denver.homelmbr.com>
> 	
> Content-Type: text/plain;	charset="iso-8859-1"
>
> Kevin,
>
> I didn't see the original post but I think this is pretty straight  
> forward and works on windows, Python 2.3.
>
> p = { (287, 0) : [0, 7],(287, 1) : [0, 7, 2],(288, 0) : [ 3, 8 ],(288,  
> 1) : [ 3, 8, 6 ],(291, 0) : [ 3, 7 ],(291, 1) : [ 6, 3, 7 ],(292, 0) :  
> [ 3, 7 ],(293, 0) : [0, 7],(294, 0) : [ 3, 8 ],(295, 0) : [ 3, 4  
> ],(295, 1) : [0, 7] }
>>>> n = {}
>>>> for key in p.keys():
> ... 	try:
> ...		n[tuple(p[key])].append(key)
> ... 	except KeyError:
> ... 		n[tuple(p[key])] = [key]
> ... 		
>>>> n
> {(3, 7): [(291, 0), (292, 0)], (3, 4): [(295, 0)], (3, 8): [(294, 0),  
> (288, 0)], (0, 7, 2): [(287, 1)], (0, 7): [(287, 0), (295, 1), (293,  
> 0)], (3, 8, 6): [(288, 1)], (6, 3, 7): [(291, 1)]}
>
> John Purser
>
> -----Original Message-----
> From: kevin parks [mailto:kp8 at mac.com]
> Sent: Monday, March 29, 2004 1:14 AM
> To: tutor at python.org
> Subject: [Tutor] histogram type thingy for dict items
>
>
> Hi all... Me again... *^-^* ... i'll try to make this the last one for
> a while. I've been at it pretty
> hard and one more important question.
>
> As you know.. :] i have been mucking with a lot of data in a dictionary
> that looks like:
>
>
> page08 = { (287, 0) : [0, 7],
> 	(287, 1) : [0, 7, 2],
> 	(288, 0) : [ 3, 8 ],
> 	(288, 1) : [ 3, 8, 6 ],
> 	(291, 0) : [ 3, 7 ],
> 	(291, 1) : [ 6, 3, 7 ],
> 	(292, 0) : [ 3, 7 ],
> 	(293, 0) : [0, 7],
> 	(294, 0) : [ 3, 8 ],
> 	(295, 0) : [ 3, 4 ],
> 	(295, 1) : [0, 7],
>   }
>
> pages and pages of it. The tuple represents a point time and the lists
> are my values for that time. It happens
> that there are many times that have the same data values [the list
> items are the same]... what i want to do
> now is take each unique value list (there are only 57 of them as my
> unique.py mod reports) and tell me
> where they occur so that (just looking at that little slice above) i
> would get (hope i don't mess thus up):
>
> [0, 7]       --> (287, 0), (293, 0), (295, 1)
> [0, 7, 2]   --> (287, 1)
> [ 3, 8 ]     --> (288, 0), (294, 0)
> [ 3, 8, 6 ] --> (288, 1)
> [ 3, 7 ]      --> (291, 0), (292, 0)
> [ 6, 3, 7]  --> (291,1)
> [ 3, 4 ] --> (295, 0)
>
>
> I other words i want a sort of histogram of each unique event telling
> me at what key they happen
> and how many of each unique event there is.
>
> Now... I know that i used to have a magic little piece of code that
> does that very thing ... and i am pretty sure
> that it was Danny Yoo that help me with that, or posted it, wonderfully
> commented, to the list. I save all kind
> of things from this list! But not too long ago i lost everything when
> my hard drive gave out and not all
> my python stuff was backed up.
>
> i've searched
> http://aspn.activestate.com/ASPN/Mail/Browse/Threaded/python-Tutor and
> the cookbook
> and i don't see anything and a google turns up some crazy
> incomprehensible very advanced bells and
> whistles graphic things that just is a heck of a lot more than i can
> wrap my pea sized brain around and is
> just way more than i need.
>
> So, if anyone has such a beast or wants to help me get started i would
> be grateful. To be honest i am
> kind of sending this in desperation *^-^* (no it's not for homework)
>
> incidentally there used to be a humongous mbox file of the entire tutor
> list archive that you could grep,
> is that gone?
>
> cheers,
> kevin




More information about the Tutor mailing list