[Tutor] counting occurrences of pairings

Israel Evans israel@lith.com
Wed, 24 Apr 2002 11:31:25 -0700


Aha...  I think I've got at least part of it now...

I found that I had to create the spaces before I started assigning values to
them in a different way than I was.  This was because I was using a
reference to a dict for every value in the mix.  This way every time I
updated the values of one, All the values got updated! So this is what I
ended up doing...


>>> atupe = ('a', 'b', 'c', 'd', 'e')
>>> btupe = ('a', 'f', 'g', 'd', 'h')
>>> ctupe = ('a', 'i', 'j', 'f', 'd')
>>> alltupe = (atupe, btupe, ctupe)
>>> alldict = {}

>>> for tupe in alltupe:
	for char in tupe:
		alldict[char] = {}

>>> for tupe in alltupe:
	for char in tupe:
		for otherchar in tupe:
			alldict[char][otherchar] = 0

>>> for tupe in alltupe:
	for char in tupe:
		for otherchar in tupe:
			alldict[char][otherchar] = alldict[char][otherchar]
+ 1

>>> for item in alldict.items():
	print item
	
('a', {'a': 3, 'c': 1, 'b': 1, 'e': 1, 'd': 3, 'g': 1, 'f': 2, 'i': 1, 'h':
1, 'j': 1})
('c', {'a': 1, 'c': 1, 'b': 1, 'e': 1, 'd': 1})
('b', {'a': 1, 'c': 1, 'b': 1, 'e': 1, 'd': 1})
('e', {'a': 1, 'c': 1, 'b': 1, 'e': 1, 'd': 1})
('d', {'a': 3, 'c': 1, 'b': 1, 'e': 1, 'd': 3, 'g': 1, 'f': 2, 'i': 1, 'h':
1, 'j': 1})
('g', {'a': 1, 'h': 1, 'd': 1, 'g': 1, 'f': 1})
('f', {'a': 2, 'd': 2, 'g': 1, 'f': 2, 'i': 1, 'h': 1, 'j': 1})
('i', {'a': 1, 'i': 1, 'j': 1, 'd': 1, 'f': 1})
('h', {'a': 1, 'h': 1, 'd': 1, 'g': 1, 'f': 1})
('j', {'a': 1, 'i': 1, 'j': 1, 'd': 1, 'f': 1})

Thanks Sean!


~Israel~


-----Original Message-----
From: Sean 'Shaleh' Perry [mailto:shalehperry@attbi.com] 
Sent: 24 April 2002 10:27 AM
To: Israel Evans
Cc: tutor@python.org
Subject: Re: [Tutor] counting occurrences of pairings

>>> dict1 = {'sean': 0, 'isreal': 0}
>>> dict2 = dict1
>>> dict2['sean'] = 2
>>> print dict2
{'isreal': 0, 'sean': 2}
>>> print dict1
{'isreal': 0, 'sean': 2}

Is what is happening.  When you put basedict into the other dictionary you
are
not making a copy you are just storing a reference to basedict.  So every
time
you write to the dictionary your changes are reflected in every other
reference.

Same thing happens with lists.