[Tutor] counting occurrences of pairings

Israel Evans israel@lith.com
Wed, 24 Apr 2002 09:40:52 -0700


Hello there, I'm attempting to keep track of the number of times items are
occur together.
I really have very little experience with statistics and the like, so this
is my lame and currently broken attempt at figuring out a couple of things.



### --Here I'm setting up my tuples of data...
>>> atupe = ('a', 'b', 'c', 'd', 'e')
>>> btupe = ('a', 'f', 'g', 'd', 'h')
>>> ctupe = ('a', 'i', 'j', 'f', 'd')

### --Now I'm collecting them all together in another tuple.
>>> alltupe = (atupe, btupe, ctupe)

### --Here I'm setting up the base dictionary so that when I...
>>> basedict = {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': 0, 'g': 0, 'h':
0, 'i': 0, 'j': 0}

### --...define this dict...
>>> griddict = {'a': basedict, 'b': basedict, 'c': basedict, 'd': basedict,
'e': basedict, 'f': basedict, 'g': basedict, 'h': basedict, 'i': basedict,
'j': basedict}

### --...I can refer to the occurrences of the pairing of these two
characters like so:
>>> griddict['a']['b']
0

### -- Now I'll try it out and count all the pairings
>>> for tupe in alltupe:
	for char in tupe:
		for otherchar in tupe:
			griddict[char][otherchar] =
griddict[char][otherchar] + 1

### --I print this stuff out to see what's going on, and everything is
multiplied by five!  Apparently the for loop counted occurrences and added
them for as many items as there were in the list.  This isn't what I
expected!  Oh the Mathematical inadequacies!  
>>> for item in griddict.items():
	print item

('a', {'a': 15, 'c': 5, 'b': 5, 'e': 5, 'd': 15, 'g': 5, 'f': 10, 'i': 5,
'h': 5, 'j': 5})
('c', {'a': 15, 'c': 5, 'b': 5, 'e': 5, 'd': 15, 'g': 5, 'f': 10, 'i': 5,
'h': 5, 'j': 5})
('b', {'a': 15, 'c': 5, 'b': 5, 'e': 5, 'd': 15, 'g': 5, 'f': 10, 'i': 5,
'h': 5, 'j': 5})
('e', {'a': 15, 'c': 5, 'b': 5, 'e': 5, 'd': 15, 'g': 5, 'f': 10, 'i': 5,
'h': 5, 'j': 5})
('d', {'a': 15, 'c': 5, 'b': 5, 'e': 5, 'd': 15, 'g': 5, 'f': 10, 'i': 5,
'h': 5, 'j': 5})
('g', {'a': 15, 'c': 5, 'b': 5, 'e': 5, 'd': 15, 'g': 5, 'f': 10, 'i': 5,
'h': 5, 'j': 5})
('f', {'a': 15, 'c': 5, 'b': 5, 'e': 5, 'd': 15, 'g': 5, 'f': 10, 'i': 5,
'h': 5, 'j': 5})
('i', {'a': 15, 'c': 5, 'b': 5, 'e': 5, 'd': 15, 'g': 5, 'f': 10, 'i': 5,
'h': 5, 'j': 5})
('h', {'a': 15, 'c': 5, 'b': 5, 'e': 5, 'd': 15, 'g': 5, 'f': 10, 'i': 5,
'h': 5, 'j': 5})
('j', {'a': 15, 'c': 5, 'b': 5, 'e': 5, 'd': 15, 'g': 5, 'f': 10, 'i': 5,
'h': 5, 'j': 5})


### -- To see what was going on I decided to do a print thingy and the
results, look like I would imagine they should, but something is up and I
haven't been able to figure it out just yet.  It's probably simple and I've
just overlooked it so I beg of you to help the blind to see!  Lead me to the
country of light and knowledge!
Does anyone have any ideas on what I'm doing wrong, and what I might do
about it?  Also If any of you have any links to references on how other
people have done similar things, I would be most appreciative.  

Thanks a bundle!

>>> for tupe in alltupe:
	for char in tupe:
		print char
		for otherchar in tupe:
			print '\t', otherchar
			
a
	a
	b
	c
	d
	e
b
	a
	b
	c
	d
	e
c
	a
	b
	c
	d
	e
d
	a
	b
	c
	d
	e
e
	a
	b
	c
	d
	e
a
	a
	f
	g
	d
	h
f
	a
	f
	g
	d
	h
g
	a
	f
	g
	d
	h
d
	a
	f
	g
	d
	h
h
	a
	f
	g
	d
	h
a
	a
	i
	j
	f
	d
i
	a
	i
	j
	f
	d
j
	a
	i
	j
	f
	d
f
	a
	i
	j
	f
	d
d
	a
	i
	j
	f
	d



~Israel~